首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >由于取消链接文件函数而引起的警告- wordpress

由于取消链接文件函数而引起的警告- wordpress
EN

Stack Overflow用户
提问于 2013-10-12 18:48:20
回答 1查看 2.3K关注 0票数 0

我使用表单在我的wordpress网站的正面张贴。我允许上传图像,并有下面的代码来处理图像。

问题:在实时服务器上测试期间,我多次收到这个警告,同时试图上传大于指定文件大小(1MB)的图像。

警告: unlink(/tmp/phpKX8Ydz) function.unlink:在第54行的/home2/base/public_html/wp-content/themes/construct/multiparts/validation/validate_third_part.php中没有这样的文件或目录

下面是上面概述的第54行的代码。

代码语言:javascript
复制
unlink($_FILES[$file]['tmp_name']);

不确定这是否重要,但这里有几个指针可能有助于更快地确定问题,如果知道的话。

  1. 它是一个简单的多部分形式,它利用隐藏的输入来传递数据。
  2. 我是在一个在线测试的服务器上调试模式设置为off。
  3. PHP版本5.3.27。托管在Hostgator。
  4. 除了这个警告故障,其他的一切仍然很好。

验证代码:主要是从这个来源

代码语言:javascript
复制
//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
}

附件代码:

代码语言:javascript
复制
//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

请提出解决办法。提前谢谢。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-10-12 19:08:50

据我所知,如果一个文件超出了PHP的限制,它将不会被写入/tmp -因此您的unlink()在大文件上总是会失败。

将@符号放在unlink之前(以抑制PHP的错误消息-反正您有自己的错误通知),或者在unlink()ing之前进行file_exists()测试。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19337885

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档