• Resolved roissya24

    (@roissya24)


    I want to set post featured image from local stored picture, but I can’t create temporary file on server.

    When I try to do that:

    require_once(ABSPATH . "wp-admin" . '/includes/image.php');
    require_once(ABSPATH . "wp-admin" . '/includes/file.php');
    require_once(ABSPATH . "wp-admin" . '/includes/media.php');
    
    $url = ABSPATH . 'import/photos/test/test.jpg'; // DON"T WORK, BUT FILE EXISTS
    // $url = 'https://example.com/import/photos/test/test.jpg'; - WORKS
    $tmp = download_url( $url );
    var_dump($tmp);

    I have that error on tmp:

    object(WP_Error)#11636 (2) { ["errors"]=> array(1) { ["http_request_failed"]=> array(1) { [0]=> string(46) "Предоставлен неверный URL." } } ["error_data"]=> array(0) { } }

    Path to stored file is correct. When if I try to import that file from web URL it upload correctly and shows me correct name of temp file string(20) "/tmp/test-jMHjga.tmp".

    Why I can’t use local server paths?

    • This topic was modified 7 years, 11 months ago by roissya24.
Viewing 2 replies - 1 through 2 (of 2 total)
  • Moderator bcworkz

    (@bcworkz)

    Because download_url() uses the WP_Http class to get remote resources through HTTP protocol. So the path must be an URL.

    Why not just use PHP copy() to make a copy where ever you want?

    Thread Starter roissya24

    (@roissya24)

    @bcworkz, thank you. Okay, I will try to use copy().

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘WordPress download_url() with local files import’ is closed to new replies.