【问题标题】:warning due to unlink file function - wordpress由于取消链接文件功能而发出警告 - wordpress
【发布时间】:2020-11-24 11:30:30
【问题描述】:

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

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

警告:unlink(/tmp/phpKX8Ydz) [function.unlink]: No such file or 目录在 /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 版本 5.3.27。托管在 Hostgator 上。
  4. 除了这个警告故障,其他一切都很好。

验证码:主要是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

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

【问题讨论】:

    标签: php wordpress


    【解决方案1】:

    据我所知,如果文件超过 PHP 的限制,它不会被写入 /tmp - 所以你的 unlink() 在大文件上总是会失败。

    在 unlink 之前放置 @ 符号(以抑制 PHP 的错误消息 - 无论如何您都有自己的错误通知)或在 unlink()ing 之前执行 file_exists() 测试。

    【讨论】:

    • 类似这样的东西.. - if(file_exists($_FILES[$file]['tmp_name'])) { @unlink($_FILES[$file]['tmp_name']); }
    • 似乎有效,但如果代码错误,请纠正我。谢谢。
    猜你喜欢
    • 1970-01-01
    • 2023-03-18
    • 2014-06-18
    • 2012-01-27
    • 2021-06-17
    • 2019-10-30
    • 1970-01-01
    • 2022-09-28
    • 1970-01-01
    相关资源
    最近更新 更多