• I am trying to grab an image from a url and get it show up in the media library using a plug-in I am writing.

    Everything works except wp_handle_upload errors out with
    ‘File is empty. Please upload something more substantial. This error could also be caused by uploads being disabled in your php.ini or by post_max_size being defined as smaller than upload_max_filesize in php.ini.’

    I’ve checked over my php.ini settings and everything seems to be OK. My max upload size is 32MB and my test file is 2.31KB. Also The file exists because when I do a rename() function and send the file where it is supposed to go, it all works.

    This is what I have so far.

    if ( ! function_exists( 'wp_handle_upload' ) ) require_once( ABSPATH . 'wp-admin/includes/file.php' );
    $upload_dir = wp_upload_dir();
    $upload_path = $upload_dir['path'].'/';
    
    $sm_img = 'http://url.to/image/location.jpg';
    $temp_path = '/local/dir/to/save/temp/image';
    
    if(!file_exists($upload_path.$sm_img))
    {
    	file_put_contents($temp_path.$sm_img, file_get_contents($this->_amz_response->Items->Item->SmallImage->URL));
    	$overrides = array('test_form'=>false);
    	$date_stuff = the_date('Y/m','','',false);
    	if(file_exists($temp_path.$sm_img))
    	{
         	     $imgloc = array(array('name'=>$sm_img,
    	               		   'tmp_name'=>$temp_path.$sm_img)
    			);
    	$save_file = wp_handle_upload($imgloc,$overrides,$date_stuff);
    	print_r($save_file);
    
    //Added this bit a test to make sure the permissions / php.ini settings were clear.
    
    if(rename($temp_path.$sm_img,$new_path))
    {
    	echo 'moved';
    }
    else
    {
    	echo 'no move';
    }
    }
    else
    {
    //echo 'temp image not found';
    }
    )

    Any suggestions on what I might be doing wrong? I am thinking it has to do with the array being built incorrectly for the image location, but I can’t seem to find anything on what that should look like.

    Thanks in advance,
    C

Viewing 1 replies (of 1 total)
  • Moderator bcworkz

    (@bcworkz)

    You’re on the right track. The file parameter you pass as $imgloc is supposed to be an element from $_FILES, not hardcoded. If you need to hardcode for whatever reason, you need to add the file size value to $imgloc under the key ‘size’, at the same level as ‘name’. As long as the value is positive and non-zero, it will pass the empty file test that is throwing the error.

    It appears the wp_handle_upload() function expects the $imgloc parameter to NOT have the outer array container, a simple associative array should suffice.

Viewing 1 replies (of 1 total)
  • The topic ‘having problems with wp_handle_upload’ is closed to new replies.