-2

i am used the follow script what will upload an image by URL to wp, but it fails (debugging brings "The uploaded file could not be moved to wp-content/uploads/2016/03"). But i was wondering... the uploaded image is 778kb, upload_max_filesize in PHP-7 php.ini was 100M. On begin of week this function works fine, but since last two days (without changing anything) this will not work.

I used PHP Version 7.0.4-5 and the function loads per CLI-Script (a cronjob). The /etc/php/7.0/cli/php.ini upload_max_filesize was changed from 2M to 100M (also post_max_size from default to 100M). The rights on directory /tmp and /var/www/domain.tld/htdocs/wp-content/uploads are all right (chmod 0775). Both directories 2016 and 03 are chowned by www-data and have also the rights 0775.

The temporary file by download_url() exists also and are a correct jpg-file. I don't know anymore where i can look to find the error. I have test it also on a live server... the same error.

Here is the function:

function setThumbnail($postid,$img,$isPreview=false) {
    include_once(ABSPATH."/wp-admin/includes/media.php");
    include_once(ABSPATH."/wp-admin/includes/file.php");
    include_once(ABSPATH."/wp-admin/includes/image.php");
    global $wpdb;
    $tmp=download_url($img);
    $filename=basename($img);
    $file_arr['name']=$filename;
    $file_arr['tmp_name']=$tmp;
    if (is_wp_error($tmp)){
        @unlink($file_arr['tmp_name']);
        $wpdb->insert($wpdb->prefix.'ctcronlog',array('created'=>current_time('timestamp'),'doing'=>'download_thumb','msg'=>$tmp->get_error_message()),array('%d','%s','%s'));
        return false;
    }
    $id=media_handle_sideload($file_arr,$postid);
    if(is_wp_error($id)){
        @unlink($file_arr['tmp_name']);
        $errors=array();
        foreach($id->get_error_messages() as $err){
            $errors[]=$err;
        }
        $wpdb->insert($wpdb->prefix.'ctcronlog',array('created'=>current_time('timestamp'),'doing'=>'upload_thumb','msg'=>var_export($errors,true)),array('%d','%s','%s'));
        return false;
    }
    $src=wp_get_attachment_url($id);
    if($isPreview) {
        set_post_thumbnail($postid,$id);
    }
    return $src;
}
1

The Problem was the @copy() in wp-includes/file.php class of wordpress. This will mask the real problem why the file could not copied (the rights on on htdocs was not ok). So the problem is fixed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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