Uploading multiple files and renaming - PHP

user4540334

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, ...

$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 hope that explained it correctly.

tomloprod

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>";
          }

     }

}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Uploading then renaming multiple files over ssh

From Dev

Uploading then renaming multiple files over ssh

From Dev

Issues with Uploading Multiple files with PHP

From Dev

PHP code for uploading multiple files

From Dev

renaming an image during uploading (PHP)

From Dev

Error in image uploading and renaming in php

From Dev

PHP Uploading Multiple Files With Captions To MySQL

From Dev

Renaming Multiple Files

From Dev

Renaming multiple files; appending

From Dev

Renaming multiple files with rename

From Dev

Renaming multiple files with Bash

From Dev

Renaming multiple files

From Dev

PHP Transliteration and renaming files

From Dev

Renaming files in PHP?

From Dev

Upload multiple files to server with php not uploading to server all files

From Dev

Upload multiple files to server with php not uploading to server all files

From Dev

Multiple files uploading in php and mysql with Multiple input fields

From Dev

Uploading multiple files in a ModelForm

From Dev

AngularJs Uploading multiple files .then

From Dev

Multiple Files Uploading in Laravel

From Dev

Python Renaming Multiple Files in Directory

From Dev

cmd command for renaming multiple files

From Dev

Renaming multiple files using sed

From Dev

Renaming multiple files at once with Python

From Dev

Renaming Multiple files in different directories

From Dev

Renaming multiple files using a loop

From Dev

Renaming Multiple Files at Once in a Directory

From Dev

Uploading multiple files in PHP using HTML Form or cURL

From Dev

Uploading multiple files in PHP using HTML Form or cURL