简体   繁体   English

上传多个文件并重命名 - PHP

[英]Uploading multiple files and renaming - PHP

I'm uploading multiple files.我正在上传多个文件。 Main function works fine, but I have to change the names of uploading files Like: name1.jpg , name2.jps , name3.jpg , ...主要功能工作正常,但我必须更改上传文件的名称,例如: name1.jpgname2.jpsname3.jpg ,...

$i = 1;
if(move_uploaded_file($_FILES['upl']['tmp_name'], 'uploads/name'.$i++.'.'.$extension)){
     echo '{"status":"success"}';
     exit;
}

Number $i should grow with amount of uploaded files.数字$i应该随着上传文件的数量而增长。 I hope that explained it correctly.我希望解释正确。

You need a loop :你需要一个循环

if(isset($_FILES['files'])){

     $name_array = $_FILES['files']['name'];
     $tmp_name_array = $_FILES['files']['tmp_name'];
     // Number of files
     $count_tmp_name_array = count($tmp_name_array);

     // We define the static final name for uploaded files (in the loop we will add an number to the end)
     $static_final_name = "name";

     for($i = 0; $i < $count_tmp_name_array; $i++){
          // Get extension of current file
          $extension = pathinfo($name_array[$i] , PATHINFO_EXTENSION);

          // Pay attention to $static_final_name 
          if(move_uploaded_file($tmp_name_array[$i], "uploads/".$static_final_name.$i.".".$extension)){
               echo $name_array[$i]." upload is complete<br>";
          } else {
               echo "move_uploaded_file function failed for ".$name_array[$i]."<br>";
          }

     }

}

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

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