错误无法解决PHP上的最大上载文件大小

时间:2014-02-09 17:15:24

标签: php mysql

我一直想知道为什么当我尝试上传大小超过2mb的文件时我没有错误?

我已经阅读了这个How to limit file upload type file size in PHP?,但仍然无效。 (注意:我在这里不像客户端验证那样使用javascript)。

这是我的代码:

<form action="" method="post" enctype="multipart/form-data">
                        <input type="text" name="title" class="form-control" placeholder="Document Title" required autofocus><br>
                        <textarea name="description" class="form-control" placeholder="Description" rows="10" cols="10"></textarea><br>
                        <input type="file" name="documento" required><br>
                        <button type="upload" name="upload" class="btn btn-default"><span class="glyphicon glyphicon-open"> </span> Upload</button>
                    </form>

然后

if (isset($_POST['upload'])) {
    $ddd_t = htmlspecialchars($_POST['title']);
    $ddd_d = htmlspecialchars($_POST['description']);
    date_default_timezone_set('Asia/Manila');
    $date_time = date('Y-m-d H:i:s') ;
    $filename = strtolower($_FILES['documento']['name']);
    $target = "../downloads/files/";
    $rand = rand(1,10000);
    $target = $target . $rand . "_" . $filename;
    $maxsize = 2097152;
    $mime_type = array(
        'application/pdf',
        'application/vnd.ms-powerpoint',
        'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
        'application/vnd.ms-excel',
        'text/plain',
        'application/vnd.openxmlformats-officedocument.presentationml.presentation',
        'application/msword'
        );
    if (empty($ddd_t)) {
        $errorize1 = "Required Title!";
    }
    else{
        if (empty($filename)) {
            $errorize1 = "Required File!";
        }
    }
    if (($_FILES['documento']['size'] > $maxsize) || ($_FILES['documento']['size'] == 0)){
        $errorize = "Max size is 2mb";
    }
    else{
        if (!in_array($_FILES['documento']['type'], $mime_type)){
            $errorize = "Invalid File. Only powerpoint, excel, pdf, word, plain-txt accepted!";
        }
    }
    if (isset($errorize1) || isset($errorize))
    {

    }
    else{
    $_FILES['documento']['name'] = $rand . "_" . $filename;
    $upload_file = ($_FILES['documento']['name']);
    $query = "INSERT INTO files(docu_title, description, link, date) VALUES(:ddd_t, :ddd_d, :document, :date_time)";
    $data = $conn->prepare($query);
    $result = $data->execute(array(':ddd_t' => $ddd_t, ':ddd_d' => $ddd_d, ':document' => $upload_file, ':date_time' => $date_time));
    $move = move_uploaded_file($_FILES['documento']['tmp_name'], $target);
    if ($result && $move) {
        $upload_img = header("location:?success=true&file=".$_FILES['documento']['name']);
    }
    else{
        echo "error!";
    }
    }
}

<?php
            if (isset($errorize)) {
                echo '<div class="alert alert-danger alert-dismissable">
                          <button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>'. $errorize .'</div>';
            }
            if (isset($errorize1)) {
                echo '<div class="alert alert-danger alert-dismissable">
                          <button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>'. $errorize1 .'</div>';
            }
            ?>

这里只有无效的mime类型正在工作..当我尝试上传超过2mb时,我不知道文件大小错误有什么问题。

2 个答案:

答案 0 :(得分:0)

在php.ini中

检查这个

upload_max_filesize
max_input_time
memory_limit
max_execution_time
post_max_size

upload_max_filesize = 2M` please increase the limit 

; Maximum allowed size for uploaded files.
; http://php.net/upload-max-filesize
upload_max_filesize = 2M  do it as you want 

答案 1 :(得分:0)

我认为您的代码放置可能就是问题所在。

在测试以下内容时,如果文档太大或类型不正确,错误消息确实正确显示。

以下是在ONE文件中。

<?php

if (isset($_POST['upload'])) {
    $ddd_t = htmlspecialchars($_POST['title']);
    $ddd_d = htmlspecialchars($_POST['description']);
    date_default_timezone_set('Asia/Manila');
    $date_time = date('Y-m-d H:i:s') ;
    $filename = strtolower($_FILES['documento']['name']);
    $target = "../downloads/files/";
    $rand = rand(1,10000);
    $target = $target . $rand . "_" . $filename;
    $maxsize = 2097152;

    $mime_type = array(
        'application/pdf',
        'application/vnd.ms-powerpoint',
        'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
        'application/vnd.ms-excel',
        'text/plain',
        'application/vnd.openxmlformats-officedocument.presentationml.presentation',
        'application/msword'
        );
    if (empty($ddd_t)) {
        $errorize1 = "Required Title!";
    }
    else{
        if (empty($filename)) {
            $errorize1 = "Required File!";
        }
    }
    if (($_FILES['documento']['size'] > $maxsize) || ($_FILES['documento']['size'] == 0)){
        $errorize = "Max size is 2mb";
    }
    else{
        if (!in_array($_FILES['documento']['type'], $mime_type)){
            $errorize = "Invalid File. Only powerpoint, excel, pdf, word, plain-txt accepted!";
        }
    }
    if (isset($errorize1) || isset($errorize))
    {

    }
    else{
    $_FILES['documento']['name'] = $rand . "_" . $filename;
    $upload_file = ($_FILES['documento']['name']);
    $query = "INSERT INTO files(docu_title, description, link, date) VALUES(:ddd_t, :ddd_d, :document, :date_time)";
    $data = $conn->prepare($query);
    $result = $data->execute(array(':ddd_t' => $ddd_t, ':ddd_d' => $ddd_d, ':document' => $upload_file, ':date_time' => $date_time));
    $move = move_uploaded_file($_FILES['documento']['tmp_name'], $target);
    if ($result && $move) {
         $upload_img = header("location:?success=true&file=".$_FILES['documento']['name']);
    }
    else{
        echo "error!";
    }
    }
}

?>

<?php
if (isset($errorize)) {
    echo '<div class="alert alert-danger alert-dismissable">
              <button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>'. $errorize .'</div>';
}
if (isset($errorize1)) {
    echo '<div class="alert alert-danger alert-dismissable">
              <button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>'. $errorize1 .'</div>';
}
?>

<form action="" method="post" enctype="multipart/form-data">
<input type="text" name="title" class="form-control" placeholder="Document Title" required autofocus><br>
<textarea name="description" class="form-control" placeholder="Description" rows="10" cols="10"></textarea><br>
<input type="file" name="documento" required><br>
<button type="upload" name="upload" class="btn btn-default"><span class="glyphicon glyphicon-open"> </span> Upload</button>
</form>