由于取消链接文件功能而发出警告 - wordpress

时间:2013-10-12 18:48:21

标签: php wordpress

我使用表单在我的wordpress网站的前端发帖。我允许上传图片并使用以下代码处理图片。

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

  

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

下面给出了上面列出的特定行54中的代码。

unlink($_FILES[$file]['tmp_name']);

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

  1. 这是一个简单的multi-part form利用隐藏的输入来传输数据。
  2. 我在LIVE服务器上在线测试,调试模式设置为关闭。
  3. PHP Version 5.3.27。主持人Hostgator。
  4. 除了这个警告故障,其他一切仍然很好。
  5. 验证码:主要是taken from this source

    //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
    

    请提出解决方案。提前谢谢。

2 个答案:

答案 0 :(得分:0)

据我所知,如果文件超过PHP的限制,它将不会写入/ tmp - 因此unlink()将始终在大文件上失败。

在取消链接之前放置@符号(以禁止PHP的错误消息 - 无论如何都有自己的错误通知)或者在unlink()之前执行file_exists()测试。

答案 1 :(得分:-1)

根据gurung代表their comment进行回答:

if(file_exists($_FILES[$file]['tmp_name'])) { @unlink($_FILES[$file]['tmp_name']); }