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 = 'http://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 string(20) "/tmp/test-jMHjga.tmp".

Why I can’t use local server paths?

Read more here: WordPress download_url() with local files import


Solution:

If you know the solution of this issue, please leave us a reply in Comment section, to update the question.


Wordpress related questions and answers: