警告:file_put_contents():在xy行的url / xyz.php中,文件名不能为空

时间:2017-02-17 08:03:22

标签: php json wordpress web-services

这里我试图揭示wordpress网站的自定义Web服务。 在我的网站上有一个部分,用户可以上传他们的简历和求职信。

我面临以下错误:

  

警告:file_put_contents():文件名不能为空   第255行的C:\ xampp \ htdocs \ project1 \ webservices \ candidate.php   {"结果":"失败","消息":"文件上传失败"}

我为此创建了一个函数:

function save_candidate_cvcover($user_id,$cv,$cover_latter)
 {
       if(!isset($cv))
       {
       $ar = array ("result" => "failed", "message" => "No  present");
       echo json_encode($ar);
       exit();
       }
       if(!isset($ext))
       {
        $ar = array ("result" => "failed", "message" => "No extension present");
      echo json_encode($ar);
    exit();
      }
$base_dir = dirname(dirname(__FILE__));
$dir = wp_upload_dir();
$image=$cv;
$images=explode(",", $image);
$data = base64_decode($images[1]);
$ext = str_replace(".","",$ext);
$fileName= time().'_'.uniqid() . ".".$ext;
$file = $dir['path'].'/'.$fileName;
$success = file_put_contents($file, $data);
$image = wp_get_image_editor($file);
$sizes_array = array(
    array('width' => 270, 'height' => 203, 'crop' => true),
    array('width' => 236, 'height' => 168, 'crop' => true),
    array('width' => 200, 'height' => 200, 'crop' => true),
    array('width' => 180, 'height' => 135, 'crop' => true),
    array('width' => 150, 'height' => 113, 'crop' => true),
);
$resize = $image->multi_resize($sizes_array, true);


$img_resized_name = isset($resize[0]['file']) ? basename($resize[0]['file']) : '';
$filename = $img_resized_name;
$filetype = wp_check_filetype(basename($filename), null);
if ($filename != '') {
    // Prepare an array of post data for the attachment.
    $attachment = array(
        'guid' => $wp_upload_dir['url'] . '/' . ($filename),
        'post_mime_type' => $filetype['type'],
        'post_title' => preg_replace('/\.[^.]+$/', '', ($filename)),
        'post_content' => '',
        'post_status' => 'inherit'
    );

    // Insert the attachment.
    $attach_id = wp_insert_attachment($attachment, $filename);

    // Make sure that this file is included, as wp_generate_attachment_metadata() depends on it.
    require_once( ABSPATH . 'wp-admin/includes/image.php' );

    // Generate the metadata for the attachment, and update the database record.
    $attach_data = wp_generate_attachment_metadata($attach_id, $filename);
    wp_update_attachment_metadata($attach_id, $attach_data);
}

add_user_list_ment($user_id,'cs_candidate_cv', $wp_upload_dir['url'] . '/' . $file);
add_user_list_ment($user_id,'cs_cover_letter', $cover_latter);
$ar = array ("result" => "success", "message" => "CV & Cover letter updated successfully");
echo json_encode($ar);
exit();}

下面是我在调用方法时使用的代码:

if($method=='get_candidate_cvcover')
{
    $user_id=esc_sql($jsonPOST->user_id);
    get_candidate_cvcover($user_id);
}
/*Function for get candidate profile*/
if($method=='save_candidate_cvcover')
{

    $user_id=esc_sql($jsonPOST->user_id);
    $fileName= time().'_'.uniqid() . ".".$ext;
    $wp_upload_dir = wp_upload_dir();
    $success = file_put_contents($file, $data);
    $name = $_FILES["cv"]["name"];
    $ext = end((explode(".", $name))); # extra () to prevent notice
    $file = $wp_upload_dir['path'].'/'.$name.'.'.$ext;
    if (move_uploaded_file($_FILES["cv"]["tmp_name"], $file))
        $file = $name.'.'.$ext;
    else{
        $ar = array ("result" => "failed", "message" => "File upload failed");
        echo json_encode($ar);
        exit();
    }

    $cover_latter=esc_sql($jsonPOST->cover_latter);
    save_candidate_cvcover($user_id,$file,$cover_latter);
}

1 个答案:

答案 0 :(得分:0)

$cache_disqus在函数外部定义,因此无法在函数中访问。

查看有关变量范围的PHP文档。