选择多个文件,但一次捕获一个

时间:2017-01-06 02:11:57

标签: php

我构建了一个功能,用户可以选择多个文件提交它们。每个文件平均大约3mb,但我的内存限制大约为32MB(我们不会超过这个)。问题是我想让用户能够根据需要选择尽可能多的文件(平均一次最多可以选择11个),但由于我的限制是32mb,所以它不能让我思考。用户是否可以根据需要选择尽可能多的文件(假设它们不是大于32mb)但是单独捕获它们(因此给人的印象是当时只提交了一个文件)?最初,当我构建我的代码时,我认为32mb仅用于一个文件(而不是总量文件)。如何使用我已有的代码实现这一目标?

<?php
include("connection.php");
/* error_reporting(0);   
$dir=substr(uniqid(),-7); */

/* error_reporting(E_ALL);
ini_set('display_errors', 1);
 */


   // do your Db stuff here to get t
  $valid_formats = array("jpg", "png", "gif", "jpeg");
$max_file_size =        32768*32000; //32000 kb


/* $path = "Prototype/uploads/"; // Upload directory
mkdir ($path, 0744); */
$count = 0;

    $skippedimages = new SplQueue();
    $imported = new SplQueue();

if (isset($_POST['search'])) {
    // Loop $_FILES to exeicute all files

    var_dump($_FILES);

    foreach ($_FILES['files']['name'] as $f => $name) {   


        if ($_FILES['files']['error'][$f] == 4) {


          continue; // Skip file if any error found
            //echo "something <br>";
        }          
        if ($_FILES['files']['error'][$f] == 0) {              
            if ($_FILES['files']['size'][$f] > $max_file_size) {
                $message[] = "$name is too large!.";
                //echo "something***************** <br>";
                continue; // Skip large files
            }
            elseif( ! in_array(pathinfo($name, PATHINFO_EXTENSION), $valid_formats) ){
                $message[] = "$name is not a valid format";
                //echo "something+++++++++++++++++++ <br>";
                //echo "$name-- ";
                continue; // Skip invalid file formats
            }
            else{ // No error found! Move uploaded files 
              //  if(move_uploaded_file($_FILES["files"]["tmp_name"][$f], $path.$name))
               // $count=$count+1; // Number of successfully uploaded file

                //echo $path.$name;

                 $image = addslashes(file_get_contents($_FILES['files']['tmp_name'][$f]));

                $image_name = addslashes($_FILES['files']['name'][$f]);

                $ext = pathinfo($image_name, PATHINFO_EXTENSION);

                $withoutExt = preg_replace('/\\.[^.\\s]{3,4}$/', '', $image_name);

                $XXXXX= $withoutExt;


            $flag = false;  
            $query = "Select * from $dbname.XXXX
                               where XXXX= '$XXXX'"; 
            $result = mysqli_query($conn,$query);

            $rowcount=mysqli_num_rows($result);

            if($rowcount>0){



                while($row = $result->fetch_assoc()) {


                    $ID =  $row['*****'];
                    $query2 = "*******";     
                    $result2 = mysqli_query($conn,$query2);
                    if(!$result2){

                        continue;
                    }else{

                        $flag =true;
                    }


                }

            if($flag) {
                ++$count; 
                $withoutExt .= "-$count";
                $imported->push($withoutExt);
                }





            }else{


            }
             //Number of successfully uploaded file

            }
        }
    }

    echo "<h3>$count files were imported <h3>";





  }




    //show success message


?>

0 个答案:

没有答案