 
  Data Structure Data Structure
 Networking Networking
 RDBMS RDBMS
 Operating System Operating System
 Java Java
 MS Excel MS Excel
 iOS iOS
 HTML HTML
 CSS CSS
 Android Android
 Python Python
 C Programming C Programming
 C++ C++
 C# C#
 MongoDB MongoDB
 MySQL MySQL
 Javascript Javascript
 PHP PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to upload multiple files and store them in a folder with PHP?
Below are the steps to upload multiple files and store them in a folder −
- Input name must be defined as an array i.e. name="inputName[]"
- Input element should have multiple="multiple" or just multiple
- In the PHP file, use the syntax "$_FILES['inputName']['param'][index]"
- Empty file names and paths have to be checked for since the array might contain empty strings. To resolve this, use array_filter() before count.
Below is a demonstration of the code −
HTML
<input name="upload[]" type="file" multiple="multiple" />
PHP
$files = array_filter($_FILES['upload']['name']); //Use something similar before processing files. // Count the number of uploaded files in array $total_count = count($_FILES['upload']['name']); // Loop through every file for( $i=0 ; $i < $total_count ; $i++ ) {    //The temp file path is obtained    $tmpFilePath = $_FILES['upload']['tmp_name'][$i];    //A file path needs to be present    if ($tmpFilePath != ""){       //Setup our new file path       $newFilePath = "./uploadFiles/" . $_FILES['upload']['name'][$i];       //File is uploaded to temp dir       if(move_uploaded_file($tmpFilePath, $newFilePath)) {          //Other code goes here       }    } } The files are listed and the count of the number of files that need to be uploaded is stored in ‘total_count’ variable. A temporary file path is created and every file is iteratively put in this temporary path that holds a folder.
Advertisements
 