Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

This is the code I am using to upload the file to WordPress

define(‘WP_DEBUG’, true);

$filename = "test.png";
$tmpFile = download_url("http://url.com/testing/crop/".$filename);

chmod($tmpFile, 0755);

$mimeType = wp_check_filetype($_SERVER['DOCUMENT_ROOT'] . '/testing/crop/'.$filename);
$file_array = array(
    'file'      => $_SERVER['DOCUMENT_ROOT'] . '/testing/crop/'.$filename,
    'url'       => $_SERVER['DOCUMENT_ROOT'] . '/testing/crop/'.$filename,
    'type'      => $mimeType['type'],
    'size'      => filesize($_SERVER['DOCUMENT_ROOT'] . '/testing/crop/'.$filename),
    'name'      => $filename,
    'tmp_name'  => $tmpFile
);

$image = wp_handle_upload($file_array, array('test_form' => FALSE, 'test_upload' => FALSE, 'test_type' => FALSE));

print_r($file_array);
print_r($image);

unlink($tmpFile);
?>

This is the error I am getting "The uploaded file could not be moved to /home/xxxx/public_html/wp-content/uploads/2012/09."

What I am trying to do is have a small PHP script outside of WordPress upload images to its Media Library and then post it however at this point I am getting stuck at the uploading stage.

All the permissions are correct as I am able to upload fine through the WordPress admin area.

Any help is much appreciated.

share|improve this question
1  
Why don’t you use a local path for the temporary file? PHP cannot copy a file to a HTTP URL. –  toscho Sep 8 '12 at 1:06
    
A local path is being used "download_url" returns "/home/xxxx/public_html/wp-content/test1.tmp" –  Rambomst Sep 8 '12 at 1:19
    
And url is not an URL but a local path too? –  toscho Sep 8 '12 at 1:24
    
url isnt being used in this instance. After looking into the error here wpseek.com/wp_handle_upload it seems to be failing at "if ( false === @ move_uploaded_file( $file['tmp_name'], $new_file ) )" for the life of me I can't work out why its failing to move the uploaded file then it throws the error. –  Rambomst Sep 8 '12 at 1:29
add comment

1 Answer

up vote 4 down vote accepted

The move_uploaded_file function is a PHP function:

http://php.net/manual/en/function.move-uploaded-file.php

One important thing to note about it from that page:

This function checks to ensure that the file designated by filename is a valid upload file (meaning that it was uploaded via PHP's HTTP POST upload mechanism). If the file is valid, it will be moved to the filename given by destination.

You're not uploading a file here, you're downloading it from a URL, saving it locally, and then attempting to use wp_handle_upload (which uses move_uploaded_file) to handle it. This fails because it's not actually an uploaded file.

What you're actually trying to do is called a "sideload", where you get a file from URL and load it in directly. WordPress has a function for this specific case, called wp_handle_sideload. For the specific case of images, WordPress has another function called media_sideload_image that does much the same thing but also handles all the image processing.

If you really are wanting to upload files and not sideload them from a URL (this could be test code you're trying to do), then you need to code up a file upload form, get the contents of the $_FILES[0] parameter, and pass that to wp_handle_upload. If you're trying specifically to handle uploaded images or other items for the media library, use media_handle_upload instead. Uploads through these function have to be real, not faked.

share|improve this answer
    
Thanks that is very helpful! –  Rambomst Sep 8 '12 at 2:01
add comment

Your Answer

 
discard

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.