繁体   English   中英

设置最大文件大小 PHP

[英]Set max file size PHP

我正在创建一个社交网站,并试图限制用户可以上传的文件的大小。 我已经知道在 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";
}

我还用$file_size替换$_FILES['files']['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();

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

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

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

$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();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM