Rounds
Rounds

Reputation: 1889

wordpress - upload a file from outside wp uploads

I want to attach a file already on server but outside the wp uploads directory to a post, so I tried the media_handle_sideload and used the download_url() on the file's URL I'm trying to attach, however for some reason, the download_url cannot access my server, is there a work around that I can do so instead of giving media_handle_sideload a $_FILES array I can give it the file absolute path? Or can I move a file already on server to the temp folder?

Upvotes: 1

Views: 758

Answers (1)

Rounds
Rounds

Reputation: 1889

I solved this by simulating a $_FILES array:

$file_array = array(
     'name' => basename( $file_path ),
     'type' => $url_type,
     'tmp_name' => $file_path,
     'error' => 0,
     'size' => filesize( $file_path )
);

And then

$att_id = media_handle_sideload( $file_array, $post_id );

Upvotes: 2

Related Questions