如何获得多个文件的PHP的总大小

时间:2018-08-17 08:46:41

标签: php html file upload


我试图用单个输入上传多个文件,但我想将所有文件的总大小限制为小于100 MB。我该怎么办?
这是我的代码:

<?php
if(isset($_POST['submit'])){
    if(count($_FILES['upload']['name']) > 0){
        for($i=0; $i<count($_FILES['upload']['name']); $i++) {
           $fileName = $_FILES['upload']['name'][$i];
           $fileExt = strtolower(pathinfo($_FILES['upload']['name'][$i],PATHINFO_EXTENSION));
           $maxFileSize = 100 * 1024 * 1024 /* 100MB */;
           if(empty($fileName)) {
             echo 'Please select photos to upload!';
           } else if(!in_array( $fileExt, array('jpg', 'jpeg', 'png', 'gif', 'bmp'))) {
             echo 'Only photos, videos and audios allowed. If you have one or more files that is not in our <a href="#">supported extensions</a> directory, please remove it!';
           } else if($_FILES['upload']['size'][$i]>$maxFileSize) {
             echo 'Your file\s exceed the limit of 100MB capacity';
           } else {
             echo "Uploaded";
           }
        }
    }
}
?>

<form action="" enctype="multipart/form-data" method="post">
  <div>
       <label for='upload'>Add Attachments:</label>
       <input id='upload' name="upload[]" type="file" multiple="multiple" />
   </div>

   <p><input type="submit" name="submit" value="Submit"></p>
 </form>

似乎我无法获得所有文件的大小总和。我怎样才能做到这一点? 并预先感谢!

2 个答案:

答案 0 :(得分:2)

如果只想将所有上载文件的大小包含到代码中,则可以使用array_sum(),而无需使用循环。

$totalFileSize = array_sum($_FILES['upload']['size']);

这将为您提供所有文件的大小(以字节为单位)。那么您可以将其与最大尺寸进行比较:

$maxFileSize = 100 * 1024 * 1024 /* 100MB */;
if ($totalFileSize > $maxFileSize) {
    echo 'Your files exceed the limit of 100MB capacity';
}

根据您的代码,这是array_sum的完整代码,以获取文件总大小

<?php
if (isset($_POST['submit'])) {
    if (count($_FILES['upload']['name']) > 0) {
        // compute the total size of the uploaded files
        $totalFileSize = array_sum($_FILES['upload']['size']);
        echo 'upload size : ' . $totalFileSize . ' bytes';
        $maxFileSize = 100 * 1024 * 1024;
        // check if the upload size is less than the max allowed
        if ($totalFileSize > $maxFileSize) {
            echo 'Your files exceed the limit of 100MB capacity';
        } else {
            // upload size is OK, process files
            for ($i = 0; $i < count($_FILES['upload']['name']); $i++) {
                $fileName = $_FILES['upload']['name'][$i];
                $fileExt = strtolower(pathinfo($_FILES['upload']['name'][$i], PATHINFO_EXTENSION));
                if (empty($fileName)) {
                    echo 'Please select photos to upload!';
                } else {
                    if (!in_array($fileExt, ['jpg', 'jpeg', 'png', 'gif', 'bmp'])) {
                        echo 'Only photos, videos and audios allowed. If you have one or more files that is not in our <a href="#">supported extensions</a> directory, please remove it!';
                    } else {
                        echo "Uploaded";
                    }
                }
            }
        }
    }
}
?>

<form action="" enctype="multipart/form-data" method="post">
    <div>
        <label for='upload'>Add Attachments:</label>
        <input id='upload' name="upload[]" type="file" multiple="multiple"/>
    </div>

    <p><input type="submit" name="submit" value="Submit"></p>
</form>

答案 1 :(得分:0)

使用变量在循环内部进行计算。我已经将 $ total_file_size 变量放在下面了。

<?php
if(isset($_POST['submit'])){
    if(count($_FILES['upload']['name']) > 0){

    $total_file_size = 0;

    for($i=0; $i<count($_FILES['upload']['name']); $i++) {

       $total_file_size += $_FILES['upload']['size'][$i];

       $fileName = $_FILES['upload']['name'][$i];
       $fileExt = strtolower(pathinfo($_FILES['upload']['name'][$i],PATHINFO_EXTENSION));
       $maxFileSize = 100 * 1024 * 1024 /* 100MB */;
       if(empty($fileName)) {
         echo 'Please select photos to upload!';
       } else if(!in_array( $fileExt, array('jpg', 'jpeg', 'png', 'gif', 'bmp'))) {
         echo 'Only photos, videos and audios allowed. If you have one or more files that is not in our <a href="#">supported extensions</a> directory, please remove it!';
       } else if($_FILES['upload']['size'][$i]>$maxFileSize) {
         echo 'Your file\s exceed the limit of 100MB capacity';
       } else {
         echo "Uploaded";
       }
    }
    }
}
?>