Multiple files upload at once with php

PHP Upload Multiple Files

Summary: in this tutorial, you’ll learn how to upload multiple files to the server in PHP securely.

Introduction to the PHP upload multiple files

In the previous tutorial, you learned how to upload a single file from a client to the webserver in PHP. All the rules of uploading a single file are relevant to multiple files.

First, the HTML form element must have the enctype attribute sets to «multipart/form-data» to enable file uploading. For example:

"index.php" method="post" enctype="multipart/form-data">Code language: PHP (php)

Second, the file input element must have the multiple attribute and its name must have the square brackets ( [] ) like this:

"file" name="files[]" >"files" multiple />Code language: PHP (php)

In PHP, you can access the $_FILES[‘files’] to get the information of the uploaded files:

 var_dump($_FILES['files']);Code language: HTML, XML (xml)

The ‘files’ is the name of the file input element.

PHP upload multiple files example

The following example reuses the existing functions and logic developed in the file upload tutorial.

First, create the following project structure:

├── inc | ├── flash.php | └── functions.php ├── index.php ├── upload.php └── uploads

Second, add the following code to the index.php to create the file upload form:

 session_start(); require_once __DIR__ . '/inc/flash.php'; ?> html> html lang="en"> head> meta charset="UTF-8"/> meta name="viewport" content="width=device-width, initial-scale=1.0"/> title>PHP upload multiple files title> link rel="stylesheet" href="https://www.phptutorial.net/app/css/style.css" /> head> body>  flash('upload') ?> main> form action="upload.php" method="post" enctype="multipart/form-data"> div> label for="files">Select files to upload: label> input type="file" name="files[]" id="files" multiple required/> div> div> button type="submit">Upload button> div> form> main> body> html>Code language: HTML, XML (xml)

The index.php does the following:

1) Start a new session or resume an existing session:

2) Load the code from the inc/flash.php file:

require_once __DIR__ . '/inc/flash.php';Code language: PHP (php)

3) Call the flash() function to show a message with the name ‘upload’ . The flash() function is defined in the flash.php file.

 flash('upload') ?>Code language: HTML, XML (xml)

4) Create an upload form that submits to the upload.php file.

Third, add the following code to the upload.php file to validate and upload multiple files:

 session_start(); require_once __DIR__ . '/inc/flash.php'; require_once __DIR__ . '/inc/functions.php'; const ALLOWED_FILES = [ 'image/png' => 'png', 'image/jpeg' => 'jpg' ]; const MAX_SIZE = 5 * 1024 * 1024; // 5MB const UPLOAD_DIR = __DIR__ . '/uploads'; $is_post_request = strtolower($_SERVER['REQUEST_METHOD']) === 'post'; $has_files = isset($_FILES['files']); if (!$is_post_request || !$has_files) < redirect_with_message('Invalid file upload operation', FLASH_ERROR); > $files = $_FILES['files']; $file_count = count($files['name']); // validation $errors = []; for ($i = 0; $i < $file_count; $i++) < // get the uploaded file info $status = $files['error'][$i]; $filename = $files['name'][$i]; $tmp = $files['tmp_name'][$i]; // an error occurs if ($status !== UPLOAD_ERR_OK) < $errors[$filename] = MESSAGES[$status]; continue; > // validate the file size $filesize = filesize($tmp); if ($filesize > MAX_SIZE) < // construct an error message $message = sprintf("The file %s is %s which is greater than the allowed size %s", $filename, format_filesize($filesize), format_filesize(MAX_SIZE)); $errors[$filesize] = $message; continue; > // validate the file type if (!in_array(get_mime_type($tmp), array_keys(ALLOWED_FILES))) < $errors[$filename] = "The file $filename is allowed to upload"; > > if ($errors) < redirect_with_message(format_messages('The following errors occurred:',$errors), FLASH_ERROR); > // move the files for($i = 0; $i < $file_count; $i++) < $filename = $files['name'][$i]; $tmp = $files['tmp_name'][$i]; $mime_type = get_mime_type($tmp); // set the filename as the basename + extension $uploaded_file = pathinfo($filename, PATHINFO_FILENAME) . '.' . ALLOWED_FILES[$mime_type]; // new filepath $filepath = UPLOAD_DIR . '/' . $uploaded_file; // move the file to the upload dir $success = move_uploaded_file($tmp, $filepath); if(!$success) < $errors[$filename] = "The file $filename was failed to move."; > > $errors ? redirect_with_message(format_messages('The following errors occurred:',$errors), FLASH_ERROR) : redirect_with_message('All the files were uploaded successfully.', FLASH_SUCCESS); Code language: HTML, XML (xml)

1) Start a new session or resume an existing one:

2) Load the code from the flash.php and functions.php :

require_once __DIR__ . '/inc/flash.php'; require_once __DIR__ . '/inc/functions.php';Code language: PHP (php)

Note that you should use the flash.php from the flash message tutorial and functions.php from the file upload tutorial.

Since the upload.php deals with multiple files, it will issue multiple error messages, one for each uploaded file.

To make it convenient, we can define a function that returns a single error message from multiple error messages like this:

And add this format_messages() function to the functions.php file.

3) Return an error message if the request method is not POST or the $_FILES does not contain the files field:

$is_post_request = strtolower($_SERVER['REQUEST_METHOD']) === 'post'; $has_files = isset($_FILES['files']); if (!$is_post_request || !$has_files) < redirect_with_message('Invalid file upload operation', FLASH_ERROR); > Code language: PHP (php)

4) Get the uploaded files from $_FILES and the number of files uploaded:

$files = $_FILES['files']; $file_count = count($files['name']);Code language: PHP (php)

5) For each uploaded file, check the error code and validate the file size, type. If an error occurred, add an error message to the $errors array, skip the current validation loop, and validate the next file.

// validation $errors = []; for ($i = 0; $i < $file_count; $i++) < // get the uploaded file info $status = $files['error'][$i]; $filename = $files['name'][$i]; $tmp = $files['tmp_name'][$i]; // an error occurs if ($status !== UPLOAD_ERR_OK) < $errors[$filename] = MESSAGES[$status]; continue; > // validate the file size $filesize = filesize($tmp); if ($filesize > MAX_SIZE) < // construct an error message $message = sprintf("The file %s is %s which is greater than the allowed size %s", $filename, format_filesize($filesize), format_filesize(MAX_SIZE)); $errors[$filesize] = $message; continue; > // validate the file type if (!in_array(get_mime_type($tmp), array_keys(ALLOWED_FILES))) < $errors[$filename] = "The file $filename is allowed to upload"; > >Code language: PHP (php)

If an error occurred, redirect back to the index.php and show the error message:

if ($errors) < redirect_with_message(format_messages('The following errors occurred:',$errors), FLASH_ERROR); > Code language: PHP (php)

Note that we use the format_messages() function to convert the $errors array to a single formatted error message.

6) If no error occurs, move each file to the upload directory:

// move the files for($i = 0; $i < $file_count; $i++) < $filename = $files['name'][$i]; $tmp = $files['tmp_name'][$i]; $mime_type = get_mime_type($tmp); // set the filename as the basename + extension $uploaded_file = pathinfo($filename, PATHINFO_FILENAME) . '.' . ALLOWED_FILES[$mime_type]; // new filepath $filepath = UPLOAD_DIR . '/' . $uploaded_file; // move the file to the upload dir $success = move_uploaded_file($tmp, $filepath); if(!$success) < $errors[$filename] = "The file $filename was failed to move."; > >Code language: PHP (php)

8) Show the error message if the move has an error. Otherwise, show a success message:

$errors ? redirect_with_message(format_messages('The following errors occurred:',$errors), FLASH_ERROR) : redirect_with_message('All the files were uploaded successfully.', FLASH_SUCCESS); Code language: PHP (php)

Summary

  • Set enctype attribute of the form to «multipart/form-data» to enable file uploading in the form.
  • Set the multiple attribute and add square brackets ( [] ) to the file input element to upload multiple files.
  • Always validate the information in the $_FILES before processing them.
  • Use the move_uploaded_file() function to move the upload files.

Источник

Multiple files upload at once with PHP

Whether you’re creating a file-sharing platform, a social media website, or any other type of web application, the ability to upload multiple files at once can significantly improve user experience.

In PHP, it is possible to upload multiple files using a single input file element. You just need to customize your single file upload PHP code and enable your file element to select multiple files.

In this tutorial, I show you how to implement multiple files upload with validation in PHP.

Multiple files upload at once with PHP

Contents

1. HTML – Create Multiple File upload

Here, are the steps to enable multiple file uploads in .

  • Create an input type=’file’ element and for enabling multiple files selection add multiple attribute. For reading all selected files when submitted add [] brackets at the end of a name that denotes an Array.

Completed Code

2. PHP – Upload Multiple selected files

When submitted then first count selected files and loop through all files. Select files on the bases of an index.

I am storing files to uploads folder. Assign file name with upload path to $location , assign file extension to $extension .

Initialize $valid_extensions Array with allowed file upload extensions. If extension exists in $valid_extensions Array then upload the file.

Completed Code

"; $totalFileUploaded++; > > > echo "Total File uploaded : ".$totalFileUploaded; >

3. Completed Code

      NOTE - Files not upload to the server only displaying names of file. 


"; $totalFileUploaded++; > > > echo "Total File uploaded : ".$totalFileUploaded; > ?>

4. Demo

5. Conclusion

You have to add [] with the file name and multiple attribute in the HTML and access the selected file using the index position and perform file upload operation using PHP.

If you are using more than one file element for a single task then you can use the above PHP script as a replacement.

Make sure to check upload_max_filesize and post_max_size values in the php.ini file if you are allowing large files to upload.

If you found this tutorial helpful then don’t forget to share.

Источник

Читайте также:  Generate reports with python
Оцените статью