PHP upload multiple files
In this example, we will show a script in PHP to upload multiple files at once. This PHP upload multiple files script works in any standard PHP based server. First of all, the HTML page will prompt you to select multiple files. Then, as you click Upload, the server will process the files one by one.
First of all, make sure the directive file_uploads is enabled in your php.ini file. This line must be set as follows:
file_uploads = On
Basically, the only variable you must change is $path, right at the beginning of this PHP script. The PHP upload multiple files script is almost ready to use. Note that you have to create the upload folder. Thus, if your server is Linux based, you may need to set write permissions on it. These permissions can be configured by FTP Online, or else, using your FTP client.
Basic PHP multiple files upload script
This first example is a basic script. Here’s the example. We have created a script named upload.php with this content:
<?php // Change this to your destination folder // Make sure to set write permissions (777) in your server. $path = "/home/mysample/public_html/upload/files/"; // If your server is Windows, use this line // $path = "C:\path\to\upload\folder"; if (isset($_POST['submit'])) { foreach($_FILES['documents']['tmp_name'] as $key => $tmp_name) { // echo "===".$key.$_FILES['documents']['name'][$key]; exit; $file_name = $key.$_FILES['documents']['name'][$key]; $file_size =$_FILES['documents']['size'][$key]; $file_tmp =$_FILES['documents']['tmp_name'][$key]; $file_type=$_FILES['documents']['type'][$key]; move_uploaded_file($file_tmp,$path.time().$file_name); echo "Processed $file_name <br>"; } echo "<br>"; echo "Finished processing files"; exit; } ?> <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" enctype="multipart/form-data"> <input name="documents[]" type="file" size="20" multiple="multiple" /> <input name="submit" type="submit" value="Upload files" /> </form>
Explanation
For instance, in our example we have placed these files:
/home/mysample/public_html/upload/upload.php – The PHP multiple files upload script
/home/mysample/public_html/upload/files/ – The folder that will receive the uploaded files
Advanced PHP multiple files upload script example
In this second example, one advanced PHP with some features. First, this advanced PHP example will include some file verifications.
This way, if the file already exists in the server, it will show an error. Second, it will check for the filesize. If it exceeds 500kb, will show an error. Then, it includes a file extension verification. This way you can enable only specific file extensions to upload.
<?php $path = "/home/mysample/public_html/upload/files/"; if (isset($_POST['submit'])) { foreach($_FILES['documents']['tmp_name'] as $key => $tmp_name) { // echo "===".$key.$_FILES['documents']['name'][$key]; exit; $file_name = $key.$_FILES['documents']['name'][$key]; $file_size =$_FILES['documents']['size'][$key]; $file_tmp =$_FILES['documents']['tmp_name'][$key]; $file_type=$_FILES['documents']['type'][$key]; $target_file = $path . basename($_FILES["documents"]["name"][$key]); // ADVANCED OPTIONS - Check if file already exists if (file_exists($target_file)) { echo "Sorry, file already exists: $file_name<br>"; continue; } // ADVANCED OPTIONS- Check file size if ($_FILES['documents']['size'][$key] > 500000) { echo "Sorry, your file is too large: $file_name<br>"; continue; } // ADVANCED OPTIONS - Allow certain file formats $imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION)); if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg" && $imageFileType != "gif" ) { echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed: $file_name<br>"; continue; } move_uploaded_file($file_tmp,$target_file); echo "Processed $file_name <br>"; } echo "<br>Finished processing files"; exit; } ?> <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" enctype="multipart/form-data"> <input name="documents[]" type="file" size="20" multiple="multiple" /> <input name="submit" type="submit" value="Upload files" /> </form>
Conclusion
PHP is perfectly compatible with file uploads. Either single or multiple file uploads. There are some PHP upload multiple files scripts. Other verifications can be added to these files. For instance, file sizes allowed extensions, etc.