1

我试图创建一个应用程序,它允许我上传大于 5 MB 的文件,但我总是收到一个错误,提示文件太大。我修改了 php.ini upload_max_filesize 和 post_max_size 并将其设置为 1GB,但它不起作用。

这是我的代码:

if(is_uploaded_file($_FILES['pdf']['tmp_name'])&&($_FILES['pdf']['error'] == UPLOAD_ERR_OK)){
    $file = $_FILES['pdf'];
    $size = round($file['size']/10485760);
    if($size > 10485760){
        echo 'The uploaded file was to large.';
    }

    if(($file['type'] != 'application/pdf') && (substr($file['name'], -4) !='.pdf')){
        echo 'The uploaded file was not a PDF.';
    }

    if(!array_key_exists('pdf', $add_pdf_errors)){
        $tmp_name = sha1($file['name'].uniqid('', TRUE));
        $dest = '../pdfs/'.$tmp_name.'_tmp';

        if(move_uploaded_file($file['tmp_name'], $dest)){
            $_SESSION['pdf']['tmp_name'] = $tmp_name;
            $_SESSION['pdf']['size'] = $size;
            $_SESSION['pdf']['file_name'] = $file['name'];
            echo '<h4>The file has been uploaded!</h4>';
        }else{
            trigger_error('The file coul not be moved.');
            unlink($file['tmp_name']);
        }
    }
}else{
    switch ($_FILES['pdf']['error']){
        case 1:
        case 2:
            echo 'The uploaded file was to large.';
            break;
        case 3:
            echo 'The file was only partially uploaded.';
            break;
        case 6:
        case 7:
        case 8:
            echo 'The file could not be uploaded to a system error.';
            break;
        case 4:
        default:
            echo 'No file was uploaded.';
            break;
    }
}

我该如何解决这个问题?

4

2 回答 2

0
$size = round($file['size']/10485760);
if($size > 10485760){
    echo 'The uploaded file was to large.';
}

这个逻辑似乎不正确。这样比较是不准确的。尝试

$size = $file['size'];
if($size > 10485760){
    echo 'The uploaded file was to large.';
}

还要检查“上传的文件是否过大”。是从您的 IF 或 ELSE 部分中显示的吗?因为在某些情况下两者都显示相同的消息,所以首先我们必须找出它在哪里显示

于 2013-01-15T19:27:35.007 回答
0

您需要调整 3 个设置:

upload_max_filesize
memory_limit
post_max_size 

全部在 php.ini 中

于 2013-01-15T19:29:07.713 回答