Change language

How to select and upload multiple files from HTML and PHP using HTTP POST?

| |
index.htmlCreate a simple HTML page for selecting multiple files and submit it to upload files to the server. Here the HTML file contains a form to select and upload files using the POST method. < html > < head > < title > Select and upload multiple files to the server < / title > < / head > < body > data is encoded as MIME data -> < form action = "file_upload.php" method = " POST " enctype = " multipart / form-data " > < h2 > Upload Files < / h2 > < p > Select files to upload: used in our PHP script -> < input type = "file" name = "files []" multiple > < br > < br > < input type = "submit" name = "submit" value = "Upload" > < / p > < / form > < / body > < / html > file_upload.phpfile_upload script .php will handle the file upload and display the upload status.  
// Check if the form was submitted if (isset ( $_ POST [ ’submit’ ])) { // Configure upload directory and allowed file types $upload_dir = ’uploads’ .DIRECTORY_SEPARATOR; $allowed_types = array ( ’jpg’ , ’png’ , ’ jpeg’ , ’gif’ ); // Determine the maximum size for files, i.e. 2 MB $maxsize = 2 * 1024 * 1024; // Checks if the user has submitted an empty form if (! empty ( array_filter ( $_ FILES [ ’ files’ ] [ ’name’ ]))) { // Loop over each file in the files [] foreach array ( $_ FILES [ ’files’ ] [ ’tmp_name’ ] as $key = > $value ) { $file_tmpname = $_ FILES [ ’files’ ] [ ’tmp_name’ ] [ $key ]; $file_name = $_ FILES [ ’files’ ] [ ’name’ ] [ $key ]; $file_size = $_ FILES [ ’files’ ] [ ’size’ ] [ $key ]; $file_ext = pathinfo ( $file_name , PATHINFO_EXTENSION); // Set file upload path $filepath = $upload_dir . $file_name ; // Check if the file type is allowed if (in_array ( strtolower ( $file_ext ), $allowed_types )) {

// Checking the file size is not more than 2 MB if ( $file_size > $maxsize ) echo " Error: File size is larger than the allowed limit. " ; // If a file with the name already exists, add the time to // before the name file to avoid overwriting the file if ( file_exists ( $filepath )) { $filepath = $upload_dir . time(). $file_name ; if (move_uploaded_file ( $file_tmpname , $filepath )) { echo "{$file_name} successfully uploaded
"
; } else { echo "Error uploading {$file_name}
"
; } } else { if (move_uploaded_file ( $file_tmpname , $filepath )) { echo "{$file_name} successfully uploaded
"
; } else { echo "Error uploading {$file_name}
"
; } } } else {

// If file extension is invalid echo "Error uploading {$file_name} " ; echo "({$file_ext } file type is not allowed)
"
; } } } else {

// If no files selected echo "No files selected. " ; } }  
?>
Output:
  • Before uploading the file:
  • After uploading the file:

Shop

Learn programming in R: courses

$

Best Python online courses for 2022

$

Best laptop for Fortnite

$

Best laptop for Excel

$

Best laptop for Solidworks

$

Best laptop for Roblox

$

Best computer for crypto mining

$

Best laptop for Sims 4

$

Latest questions

NUMPYNUMPY

Common xlabel/ylabel for matplotlib subplots

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

12 answers

NUMPYNUMPY

Flake8: Ignore specific warning for entire file

12 answers

NUMPYNUMPY

glob exclude pattern

12 answers

NUMPYNUMPY

How to avoid HTTP error 429 (Too Many Requests) python

12 answers

NUMPYNUMPY

Python CSV error: line contains NULL byte

12 answers

NUMPYNUMPY

csv.Error: iterator should return strings, not bytes

12 answers

News


Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

sin

How to specify multiple return types using type-hints

exp

Printing words vertically in Python

exp

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries

cos

Python add suffix / add prefix to strings in a list

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

Python - Move item to the end of the list

Python - Print list vertically