2017-02-17 14 views
1

Здесь я пытаюсь разоблачить пользовательские веб-службы сайта wordpress. На моем веб-сайте есть раздел, где пользователь может загрузить свое резюме и сопроводительное письмо.Предупреждение: file_put_contents(): Имя файла не может быть пустым в url/xyz.php on line xyz

Я столкнулся ниже ошибки:

Warning: file_put_contents(): Filename cannot be empty in C:\xampp\htdocs\project1\webservices\candidate.php on line 255 {"result":"failed","message":"File upload failed"}

Я создал одну функцию для этого:

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); 
} 
+0

check http://stackoverflow.com/questions/23435307/file-put-contents-filename-cannot-be-empty – Akintunde007

+0

Спасибо Кармин, но я уже посетил эту ссылку. И это не полезно для меня. :( –

+0

Guys Спасибо за вашу помощь и усилия. Моя проблема решена. Я создал для нее новую функцию. :) –

ответ

0

$cache_disqus определяется вне вашей функции и поэтому недоступен в функции.

Ознакомьтесь с документацией PHP по области переменных.

+0

Hi Amit благодарит за ваш ответ, но ваше решение похоже на http://stackoverflow.com/questions/23435307/file- put-contents-filename-не может быть пустым, и это не полезно для меня. –