【发布时间】:2017-02-17 08:03:22
【问题描述】:
在这里,我试图公开 wordpress 网站的自定义 Web 服务。 在我的网站上有一个部分,用户可以上传他们的简历和求职信信息。
我面临以下错误:
警告:file_put_contents():文件名不能为空 C:\xampp\htdocs\project1\webservices\candidate.php 在第 255 行 {"result":"failed","message":"文件上传失败"}
我为此创建了一个函数:
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);
}
【问题讨论】:
-
感谢 Carmine,但我已经访问了该链接。这对我没有帮助。:(
-
伙计们感谢您的帮助和努力。我的问题解决了。我为此创建了新功能。 :)
标签: php json wordpress web-services