我使用表单在我的 wordpress 网站的前端发布。我允许上传图片并使用以下代码来处理图片。
问题:在实时服务器上进行测试期间,我在尝试上传大于指定文件大小 (1 MB) 的图像时多次收到此警告。
警告:取消链接(/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']);
不确定这是否重要,但这里有几个指针,如果已知,可能有助于更快地确定问题。
- 它是一种简单的多部分形式,利用隐藏的输入来传递数据。
- 我在调试模式设置为关闭的 LIVE 服务器上在线测试它。
- PHP 版本 5.3.27。托管在 Hostgator 上。
- 除了这个警告故障,其他一切仍然很好。
验证码:大部分取自此来源。
//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
请提出解决方案。提前致谢。