使用数组或for循环的php多文件上载

使用数组或for循环的php多文件上载,php,Php,下面我有一个通过php上传单个文件输入的工作示例,现在我想通过遍历每个输入type=“file”来上传多个文件 我已经了解到,IE10下面不支持输入标签上的multiple属性,因此我认为最好的方法是使用几个input type=文件来循环使用它们 谢谢你的帮助 <input type="file" name="FilesUpload1" class="filesUpload" /> <input type="file" name="FilesUpload2" class="f

下面我有一个通过php上传单个文件输入的工作示例,现在我想通过遍历每个输入type=“file”来上传多个文件

我已经了解到,IE10下面不支持输入标签上的multiple属性,因此我认为最好的方法是使用几个input type=文件来循环使用它们

谢谢你的帮助

<input type="file" name="FilesUpload1" class="filesUpload" />
<input type="file" name="FilesUpload2" class="filesUpload" />
<input type="file" name="FilesUpload3" class="filesUpload" />

<?php
//Сheck that we have a file
if((!empty($_FILES["FilesUpload1"])) && ($_FILES['FilesUpload1']['error'] == 0)) {

    //Check if the file is JPEG image and it's size is less than 350Kb
    $filename = basename($_FILES['FilesUpload1']['name']);
    $ext = substr($filename, strrpos($filename, '.') + 1);

    //check file extension
    if ((($ext == "gif") 
    || ($ext == "jpeg")
    || ($ext == "jpg")
    || ($ext == "png")
    || ($ext == "doc")
    || ($ext == "docx")
    || ($ext == "rtf")
    || ($ext == "txt")          
    || ($ext == "pdf"))

    //check file mime
    && (($_FILES["FilesUpload1"]["type"] == "image/gif")
    || ($_FILES["FilesUpload1"]["type"] == "image/jpeg")
    || ($_FILES["FilesUpload1"]["type"] == "image/jpg")
    || ($_FILES["FilesUpload1"]["type"] == "image/pjpeg")
    || ($_FILES["FilesUpload1"]["type"] == "image/x-png")
    || ($_FILES["FilesUpload1"]["type"] == "image/png")
    || ($_FILES["FilesUpload1"]["type"] == "application/msword")
    || ($_FILES["FilesUpload1"]["type"] == "application/vnd.openxmlformats-officedocument.wordprocessingml.document")
    || ($_FILES["FilesUpload1"]["type"] == "application/rtf")
    || ($_FILES["FilesUpload1"]["type"] == "text/plain")
    || ($_FILES["FilesUpload1"]["type"] == "application/pdf"))


    //check file size is less than 1048576 bytes [1 MB]
    && (($_FILES["FilesUpload1"]["size"] < 1048576))) {

        //Determine the path to which we want to save this file
        $newname = dirname(__FILE__).'/../entries/'.$_POST["CompanyName"].'-'.$filename;    

        //Check if the file with the same name is already exists on the server
        if (!file_exists($newname)) {

            //Attempt to move the uploaded file to it's new place
            if ((move_uploaded_file($_FILES['FilesUpload1']['tmp_name'],$newname))) {
                echo "It's done! Your file has been saved.";

            } else {
                echo "Error: A problem occurred during file upload!";
            }

        } else {
            echo "Error: File ".$_FILES["FilesUpload1"]["name"]." already exists";
        }

    } 

    else {
        echo "Error: Only .gif, .jpeg, .jpg, .png, .doc, .docx, .rtf, .txt, .pdf files under 1 MB are accepted for upload.";
    }

} else {
    echo "Error: No file uploaded";
}

?>

您尝试过这种逻辑吗

HTML

<form method="POST" action="desired_url" enctype="multipart/form-data">

        <input type="file" name="FilesUpload[]" class="filesUpload" />
        <input type="file" name="FilesUpload[]" class="filesUpload" />
        <input type="file" name="FilesUpload[]" class="filesUpload" />

        <button type="submit" name="upload_file" class="btn btn-primary">Upload</button>
</form>

上传
PHP

if( isset($_POST["upload_file"] ) ){

    $get_files = $_FILES["FilesUpload"];

    foreach( $get_files['name'] as $key => $name ){

        $files = $_FILES['FilesUpload'];

        if( $files['error'][$key] == 0 ){

            $ext          = substr($name, strrpos($name, '.') + 1);
            $filename     = rand(11111,99999).'.'.$ext;
            $destination = "uploads/".$filename;

            if( move_uploaded_file( $files['tmp_name'][$key], $destination ) ){
                echo "Your file has been uploaded.<br>";
            }
        }

    }   

}
if(isset($\u POST[“上传文件”])){
$get_files=$_files[“FilesUpload”];
foreach($get_files['name']作为$key=>$name){
$files=$_文件['FilesUpload'];
如果($files['error'][$key]==0){
$ext=substr($name,strrpos($name,'.')+1);
$filename=rand(111119999)。“.$ext;
$destination=“uploads/”$filename;
如果(移动上传的文件($files['tmp\u name'][$key],$destination)){
echo“您的文件已上载。
”; } } } }
您尝试过这种逻辑吗

HTML

<form method="POST" action="desired_url" enctype="multipart/form-data">

        <input type="file" name="FilesUpload[]" class="filesUpload" />
        <input type="file" name="FilesUpload[]" class="filesUpload" />
        <input type="file" name="FilesUpload[]" class="filesUpload" />

        <button type="submit" name="upload_file" class="btn btn-primary">Upload</button>
</form>

上传
PHP

if( isset($_POST["upload_file"] ) ){

    $get_files = $_FILES["FilesUpload"];

    foreach( $get_files['name'] as $key => $name ){

        $files = $_FILES['FilesUpload'];

        if( $files['error'][$key] == 0 ){

            $ext          = substr($name, strrpos($name, '.') + 1);
            $filename     = rand(11111,99999).'.'.$ext;
            $destination = "uploads/".$filename;

            if( move_uploaded_file( $files['tmp_name'][$key], $destination ) ){
                echo "Your file has been uploaded.<br>";
            }
        }

    }   

}
if(isset($\u POST[“上传文件”])){
$get_files=$_files[“FilesUpload”];
foreach($get_files['name']作为$key=>$name){
$files=$_文件['FilesUpload'];
如果($files['error'][$key]==0){
$ext=substr($name,strrpos($name,'.')+1);
$filename=rand(111119999)。“.$ext;
$destination=“uploads/”$filename;
如果(移动上传的文件($files['tmp\u name'][$key],$destination)){
echo“您的文件已上载。
”; } } } }
试试这种方法

HTML:

<input name="upload[]" type="file" multiple="multiple" />
$total = count($_FILES['upload']['name']);

// Loop through each file
for($i=0; $i<$total; $i++) {

  //Get the temp file path
  $tmpFilePath = $_FILES['upload']['tmp_name'][$i];

  //Make sure we have a filepath
  if ($tmpFilePath != ""){
    //Setup our new file path
    $newFilePath = "./uploadFiles/" . $_FILES['upload']['name'][$i];

    //Upload the file into the temp dir
    if(move_uploaded_file($tmpFilePath, $newFilePath)) {

      //Handle other code here

    }
  }
}

PHP代码:

<input name="upload[]" type="file" multiple="multiple" />
$total = count($_FILES['upload']['name']);

// Loop through each file
for($i=0; $i<$total; $i++) {

  //Get the temp file path
  $tmpFilePath = $_FILES['upload']['tmp_name'][$i];

  //Make sure we have a filepath
  if ($tmpFilePath != ""){
    //Setup our new file path
    $newFilePath = "./uploadFiles/" . $_FILES['upload']['name'][$i];

    //Upload the file into the temp dir
    if(move_uploaded_file($tmpFilePath, $newFilePath)) {

      //Handle other code here

    }
  }
}
$total=count($_文件['upload']['name']);
//循环浏览每个文件
对于($i=0;$i请尝试这种方式

HTML:

<input name="upload[]" type="file" multiple="multiple" />
$total = count($_FILES['upload']['name']);

// Loop through each file
for($i=0; $i<$total; $i++) {

  //Get the temp file path
  $tmpFilePath = $_FILES['upload']['tmp_name'][$i];

  //Make sure we have a filepath
  if ($tmpFilePath != ""){
    //Setup our new file path
    $newFilePath = "./uploadFiles/" . $_FILES['upload']['name'][$i];

    //Upload the file into the temp dir
    if(move_uploaded_file($tmpFilePath, $newFilePath)) {

      //Handle other code here

    }
  }
}

PHP代码:

<input name="upload[]" type="file" multiple="multiple" />
$total = count($_FILES['upload']['name']);

// Loop through each file
for($i=0; $i<$total; $i++) {

  //Get the temp file path
  $tmpFilePath = $_FILES['upload']['tmp_name'][$i];

  //Make sure we have a filepath
  if ($tmpFilePath != ""){
    //Setup our new file path
    $newFilePath = "./uploadFiles/" . $_FILES['upload']['name'][$i];

    //Upload the file into the temp dir
    if(move_uploaded_file($tmpFilePath, $newFilePath)) {

      //Handle other code here

    }
  }
}
$total=count($_文件['upload']['name']);
//循环浏览每个文件

对于($i=0;$i退出:),我认为这非常适合您。检查此链接是否有人可以帮助我了解数组逻辑?检查:,我认为这非常适合您。检查此链接是否有人可以帮助我了解数组逻辑?