PHP upload_max_filesize& post_max_size问题

时间:2014-04-29 15:07:54

标签: php file upload

我有一个php上传文件进程,效果很好。但是我想添加一些验证以确保没有上传大文件,但是当我测试时我发现了一些奇怪的行为;

我设置了post_max_size& upload_max_filesize到5M(重启服务器)但发现如果我上传的文件大于5M,POST数据就丢失了,所以我无法检查$ _FILES ['uploadedFile'] ['size']

我确实通过确保post_max_size&来解决了这个问题。 upload_max_filesize被设置为10M(假设当然没有人会愚蠢地尝试比这更大的文件 - 它真的用于文本文件,所以他们不应该)之后我可以毫无问题地进行我的大小验证;

if($_FILES['uploadedFile']['size'] < 3072000)
{                   
    //do upload stuff                   
}
else
{                   
    $errorMessage .= 'File size to big, exceeds 3M Limit.';
}

我认为问题是POST被删除了,因为它超过了post_max_size ???我搜索了很多页面,但看不到其他人有这个问题,所以我不确定我的假设是否正确。如果我上传的文件大于10Mm,我仍然会遇到问题。

1 个答案:

答案 0 :(得分:0)

你是对的。超出大小限制时,将忽略上载的数据。因此,您无法访问['size']信息。

相反,您需要像这样验证您的上传:

<?php

if ($_FILES['uploadedFile']['error'] == UPLOAD_ERR_OK)
{
    // success - move uploaded file and process stuff here
}
else
{
    // error
    switch ($_FILES['uploadedFile']['error'])
    {
        case UPLOAD_ERR_INI_SIZE:
            echo "The uploaded file exceeds the upload_max_filesize directive in php.ini."; break;

        case UPLOAD_ERR_FORM_SIZE:
            echo "The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form."; break;

        case UPLOAD_ERR_PARTIAL:
            echo "The uploaded file was only partially uploaded. "; break;

        // etc... see the full list here http://uk1.php.net/manual/en/features.file-upload.errors.php
    }
}