Wordpress wp\生成\附件\元数据中断循环

Wordpress wp\生成\附件\元数据中断循环,wordpress,Wordpress,我以编程方式在for循环中将图像上传到wordpress的媒体库。一切正常,直到我调用wp\u generate\u attachment\u metadata,这打破了循环,似乎导致文件重新上传,并以500内部服务器错误结束 当我删除wp\u生成\u附件\u元数据时,脚本运行正常 当我运行脚本并上传文件时,wp\u generate\u attachment\u metadata按预期工作并返回正确的ID private function uploadImage($image_url) {

我以编程方式在for循环中将图像上传到wordpress的媒体库。一切正常,直到我调用
wp\u generate\u attachment\u metadata
,这打破了循环,似乎导致文件重新上传,并以
500内部服务器错误结束

当我删除
wp\u生成\u附件\u元数据时,脚本运行正常

当我运行脚本并上传文件时,
wp\u generate\u attachment\u metadata
按预期工作并返回正确的ID

private function uploadImage($image_url) {

       $upload_dir = wp_upload_dir();

       $image_data = file_get_contents($image_url);

       $filename = basename($image_url);

       if(wp_mkdir_p($upload_dir['path']))   {
        $fileExists = is_file( $upload_dir['path'] . '/' . $filename);

        $file = $upload_dir['path'] . '/' . $filename;

    } else {                                    
        $fileExists = is_file( $upload_dir['basedir'] . '/' . $filename);

        $file = $upload_dir['basedir'] . '/' . $filename;
    }

    if($fileExists) {

       global $wpdb;

       $prepare = $wpdb->prepare("SELECT * FROM $wpdb->posts WHERE post_title LIKE '%s'", '%'.$filename.'%');

       $posts = $wpdb->get_results($prepare );

       $attach_id = $posts[0]->ID;

       if(!$attach_id) {

        $wp_filetype = wp_check_filetype($file, null );

        $attachment = array(
            'post_mime_type' => $wp_filetype['type'],
            'post_title' => sanitize_file_name($filename),
            'post_content' => '',
            'post_status' => 'inherit'
            );

        var_dump('here');
        $attach_id =  wp_insert_attachment( $attachment, $file);

       $attach_data = wp_generate_attachment_metadata( $attach_id, $file );

       wp_update_attachment_metadata( $attach_id, $attach_data );
    }

    return  $attach_id; 

    } else {

        file_put_contents($file, $image_data);

        $wp_filetype = wp_check_filetype($file, null );

        $attachment = array(
            'post_mime_type' => $wp_filetype['type'],
            'post_title' => sanitize_file_name($filename),
            'post_content' => '',
            'post_status' => 'inherit'
            );


        $attach_id =  wp_insert_attachment( $attachment, $file);

       $attach_data = wp_generate_attachment_metadata( $attach_id, $file );

        wp_update_attachment_metadata( $attach_id, $attach_data );

        return $attach_id;
    }


    } 
这在循环中被称为:

 foreach ($property->pictures[0]->picture as $picture) {

        $imageID = $this->uploadImage($picture->filename);

        if($imageID){
          array_push($images,$imageID);  
      }

我运行你的代码没有任何问题。您在日志中看到任何PHP错误了吗?