WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am using this code to try to upload images located in my server to Media Library of my wordpress installation.


    $id = 0;
    $image = $_SERVER['DOCUMENT_ROOT'] . "/image01.jpg";
    $array = array( //array to mimic $_FILES
            'name' => basename($image), //isolates and outputs the file name from its absolute path
            'type' => 'image/jpeg', //yes, thats sloppy, see my text further down on this topic
            'tmp_name' => $image, //this field passes the actual path to the image
            'error' => 0, //normally, this is used to store an error, should the upload fail. but since this isnt actually an instance of $_FILES we can default it to zero here
            'size' => filesize($image) //returns image filesize in bytes

    media_handle_sideload($array, $id); //the actual image processing, that is, move to upload directory, generate thumbnails and image sizes and writing into the database happens here

I can see that this code takes my image called "image01.jpg" located in that path and then it put the image to /wp-contents/uploads/. However when I go to the admin panel in wordpress to see the image in the Media Library I cannot see the image. Also there is not any row in the wp_posts of this image.


I used this time this code:

$result = media_handle_sideload($array, $id);
echo "Finish media handle sideload<br>";

if ( ! is_wp_error( $result ) ) {
    echo "Result<br>";
} else {
    echo "Error<br>";
echo "Finish script<br>";

Nothing is shown is the execution. I can't see the msg "Finish media handle sideload". However the file is moved from path specified in "$image" to "wp-content/uploads". Note that the files is moved, not copied, so if I execute the script without copy again the image to the source (path of $image), I can see some warnings about filesize function likes this:

Warning: filesize(): stat failed for C:/Users/dlopez/Documents/Development/web-folders/image01.jpg in C:\Users\dlopez\Documents\Development\web-folders\wp_test\wp-content\plugins\pods-csv-importer\upload.php on line 16

share|improve this question
look at wp_insert_attachment – Wyck Jan 31 '14 at 0:45
Thank you, this works! – dlopezgonzalez Jan 31 '14 at 10:46

Your failing and getting a WP_Error object returned before the function runs wp_insert_attachment().

You should always include a check for is_wp_error( $thing ) when calling a function that returns WP_Errors.

$result = media_handle_sideload($array, $id);

if ( ! is_wp_error( $result ) ) {
   return $result;
} else {
   var_dump( $result->get_error_messages );

Your problem will be listed in the output of error messages

share|improve this answer
This does not work by the moment. I have updated the entry to give more details using your solution. – dlopezgonzalez Jan 31 '14 at 10:39

You shouldn't assign a string valus to image. According to the example in this page, at first you must download files to temporary directory using download_url() and assign object returned by this function to $image:

$image = download_url("http://yoursite.com/image01.jpg");

Don't forget to unlink() the $image at the end of function.

share|improve this answer

Does the form that the file is coming from use:



<form method="post" enctype="multipart/form-data">
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.