我使用表单在我的wordpress网站的正面张贴。我允许上传图像,并有下面的代码来处理图像。
问题:在实时服务器上测试期间,我多次收到这个警告,同时试图上传大于指定文件大小(1MB)的图像。
警告: unlink(/tmp/phpKX8Ydz) function.unlink:在第54行的/home2/base/public_html/wp-content/themes/construct/multiparts/validation/validate_third_part.php中没有这样的文件或目录
下面是上面概述的第54行的代码。
unlink($_FILES[$file]['tmp_name']);不确定这是否重要,但这里有几个指针可能有助于更快地确定问题,如果知道的话。
验证代码:主要是从这个来源。
//some minor form validations
if (isset($_POST['submit-3'])) {
$error = "";
//checking other stuffs
// image validation starts
if ($_FILES) {
foreach ($_FILES as $file => $array) {
//Check if the $_FILES is set and if the size is > 0 (if =0 it's empty)
if(isset($_FILES[$file]) && ($_FILES[$file]['size'] > 0)) {
$tmpName = $_FILES[$file]['tmp_name'];
list($width, $height, $type, $attr) = getimagesize($tmpName);
if ($_FILES[$file]["size"] >= 1000000) {
$error .= 'Image file is too large. Image size must be within 1MB only.!<br />';
unlink($_FILES[$file]['tmp_name']);
}
// Get the type of the uploaded file. This is returned as "type/extension"
$arr_file_type = wp_check_filetype(basename($_FILES[$file]['name']));
$uploaded_file_type = $arr_file_type['type'];
// Set an array containing a list of acceptable formats
$allowed_file_types = array('image/jpg','image/jpeg','image/gif','image/png');
// If the uploaded file is the right format
if(in_array($uploaded_file_type, $allowed_file_types)) {
} else { // wrong file type
$error .= "Accepted image formats only JPG, JPEG, GIF, or PNG files only. <br />";
}
} else {
$error .= "Please add an image<br />";
}
} // end for each
} // end if
}附件代码:
//attachment helper function
function insert_attachment($file_handler,$post_id,$setthumb='false') {
if ($_FILES[$file_handler]['error'] !== UPLOAD_ERR_OK){ return __return_false();
}
require_once(ABSPATH . "wp-admin" . '/includes/image.php');
require_once(ABSPATH . "wp-admin" . '/includes/file.php');
require_once(ABSPATH . "wp-admin" . '/includes/media.php');
$attach_id = media_handle_upload( $file_handler, $post_id );
//set post thumbnail
if ($setthumb) update_post_meta($post_id,'_thumbnail_id',$attach_id);
return $attach_id;
}
//imsert image
if ($_FILES) {
foreach ($_FILES as $file => $array) {
$newupload = insert_attachment($file,$pid);
// $newupload returns the attachment id of the file
}
} // end of attachment statement请提出解决办法。提前谢谢。
发布于 2013-10-12 19:08:50
据我所知,如果一个文件超出了PHP的限制,它将不会被写入/tmp -因此您的unlink()在大文件上总是会失败。
将@符号放在unlink之前(以抑制PHP的错误消息-反正您有自己的错误通知),或者在unlink()ing之前进行file_exists()测试。
https://stackoverflow.com/questions/19337885
复制相似问题