【问题标题】:Set max file size PHP设置最大文件大小 PHP
【发布时间】:2021-07-16 04:04:00
【问题描述】:

我正在创建一个社交网站,并试图限制用户可以上传的文件的大小。我已经知道在 wamp/xamp 设置中更改upload_max_filesize,但是有没有办法手动进行呢?例如设置最大值为 8mb 但我可以手动将其设置为 7.5mb 或 PHP 文件本身的内容吗?这就是我尝试过的方式:

$file_size = $_FILES['files']['size'];
$maxsize = 1048576;

if($_FILES['files']['size'] > $maxsize) {

    $errors = "Your file is to large";
}

我还将$_FILES['files']['size'] 替换为$file_size。我也试过了:

if ($file_size > 15097152) {

    $errors = 'File cannot be larger than 1MB';
}

谢谢。

编辑


<input name="files[]" id='files' accept="image/png,image/gif,image/jpeg" type="file" multiple="multiple" />

$date_time = date('Y-m-d_H-i-s');
$img_limit = 10;
$maxsize = 3367463;

if(isset($_POST['submit'])) {

    $errors = [];
    $file_name = count($_FILES['files']['name']);
    $file_size = $_FILES['files']['size'];
    
    //$file_type = $_FILES['files']['type'];
    $imageFileType = pathinfo($file_name, PATHINFO_EXTENSION);

    if (strtolower($imageFileType) != "jpeg"&& strtolower($imageFileType) != "jpg" && 
        strtolower($imageFileType) != "png" && strtolower($imageFileType) != "gif") {

        $errors = "File type not allowed.";
    }

    if($_FILES['files']['size'] >= $maxsize) {

        $errors = "Your file is to large";
    }

    if ($img_limit > 10) {

        $errors = 'Cannot upload more than 10 images';
    }
    // Loop through each file
    for( $i=0 ; $i < $file_name ; $i++ ) {

        //Get the temp file path
        $file_tmp = $_FILES['files']['tmp_name'][$i];

        //Make sure we have a file path
        if (!$errors || $file_tmp != "") {

            $picToUpload = $date_time . " -#- " . md5($file_name) . " -#- " . $_FILES['files']['name'][$i];
            $uploadPicture = move_uploaded_file($file_tmp, "uploads/" . $picToUpload);

            $file_path = "uploads/" . $picToUpload;

            $stmt = $con->prepare("INSERT INTO images (image) VALUES (?)");
            $stmt->bind_param('s', $file_path);
            $stmt->execute();
            //$stmt->close();
            header('Location: index4.php');
            exit();
        }
    }
}

$_SESSION['error'] = '<b><p style="color: #000; font-size: 30px; top: 34%;right: 50%;position: absolute;">
' . $errors . '</p></b>';
header('Location: index4.php');
exit();

【问题讨论】:

  • 那么,当您尝试这样做时,究竟发生了什么?当您设置$maxsize 时,已经为时已晚 - 上传(或尝试上传)已经发生。只有当 $maxsize 始终低于 upload_max_filesize 时,您的想法才会有所帮助。
  • $file_size 的值是多少?使用 else 语句中的输出对其进行调试。
  • @ADyson 唯一发生的是文件上传成功。我尝试将$maxsize 放在isset 函数之外,但结果仍然相同
  • @NikolayShindarov 我将文件上传到我的 index3.php 页面,然后将表单提交到 index4.php,所以我看不到 $file_size 的值,但图像的大小是3.1mb
  • 您似乎已经从单个文件上传更改为多个文件,但您并没有将所有验证检查都移到循环中。

标签: php wamp


【解决方案1】:

您需要将验证检查放在处理每个文件的for 循环中。

另外,您对图像数量的限制是错误的。您需要比较上传到 $img_limit 的文件数,而不是比较 $img_limit 与您初始化它时使用的相同值。

我已经采取了重定向并退出循环,因为上传第一个文件后会重定向。我也将prepare 和bind_param 排除在循环之外,因为每个文件都可以使用相同的预处理语句。

$date_time = date('Y-m-d_H-i-s');
$img_limit = 10;
$maxsize = 3367463;

if(isset($_POST['submit'])) {
    $errors = '';
    $file_count = count($_FILES['files']['name']);

    if ($file_count > $img_limit) {
        $errors = 'Cannot upload more than 10 images';
    }
    if (!$errors) {
        $stmt = $con->prepare("INSERT INTO images (image) VALUES (?)");
        $stmt->bind_param('s', $file_path);
        // Loop through each file
        for( $i=0 ; $i < $file_count ; $i++ ) {
            $file_name = $_FILES['files']['name'][$i];
            $file_size = $_FILES['files']['size'][$i];
            $file_tmp = $_FILES['files']['tmp_name'][$i];
        
            $imageFileType = strtolower(pathinfo($file_name, PATHINFO_EXTENSION));

            if ($file_size >= $maxsize) {
                $errors = "Your file is too large";
            } elseif ($imageFileType != "jpeg" && $imageFileType != "jpg" && 
                      $imageFileType != "png" && $imageFileType != "gif") {
                $errors = "File type not allowed.";
            }
            //Make sure we have a file path
            if (!$errors && $file_tmp != "") {

                $picToUpload = $date_time . " -#- " . md5($file_name) . " -#- " . $_FILES['files']['name'][$i];
                $uploadPicture = move_uploaded_file($file_tmp, "uploads/" . $picToUpload);

                $file_path = "uploads/" . $picToUpload;
                $stmt->execute();
            }
        }
    }
}
if ($errors) {
    $_SESSION['error'] = '<b><p style="color: #000; font-size: 30px; top: 34%;right: 50%;position: absolute;">
' . $errors . '</p></b>';
}
header('Location: index4.php');
exit();

【讨论】:

  • 同样的事情仍然发生。图片上传
  • 图片是否大于3MB?
  • 我的代码限制了每张图片的大小。您是否试图限制所有这些的组合大小?
  • 您可以使用array_sum($_FILES['files']['size'])获取总大小。
  • 我知道它是怎么发生的。当我将header(); exit; 移出循环时,我在编辑器中使用旧版本的答案进行操作。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-12-16
  • 1970-01-01
  • 2016-09-29
  • 2019-12-05
  • 2012-04-23
相关资源
最近更新 更多