How can you upload multiple files in PHP and store them in $_FILES?
To upload multiple files in PHP and store them in the $_FILES superglobal array, you can use the 'multiple' attribute in the HTML form input field for file uploads. This attribute allows users to select multiple files at once. In PHP, you can access these files in the $_FILES array using the input field's name attribute as an array. You can then loop through each file to handle or process them accordingly.
<form action="upload.php" method="post" enctype="multipart/form-data">
<input type="file" name="files[]" multiple>
<input type="submit" value="Upload">
</form>
<?php
if(isset($_FILES['files'])){
$files = $_FILES['files'];
foreach($files['name'] as $key => $name){
$tmp_name = $files['tmp_name'][$key];
$file_size = $files['size'][$key];
$file_type = $files['type'][$key];
// Process or store the file as needed
}
}
?>
Keywords
Related Questions
- What are common pitfalls when implementing a CAPTCHA in a PHP contact form?
- How can PHP be optimized to avoid unnecessary calculations when checking for whole numbers in a quotient?
- What considerations should be taken into account when using the crypt function in PHP for hashing passwords from /etc/shadow?