uploads - media_handle_sideload on a file already on server

admin2025-01-07  4

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?

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?

Share Improve this question asked Jun 7, 2014 at 17:23 RoundsRounds 1919 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

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 );

As simple as this..

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1736257202a420.html

最新回复(0)