What is the correct way to handle multiple file uploads in PHP using the $_FILES array?
When handling multiple file uploads in PHP using the $_FILES array, you need to loop through each file in the array and process them individually. This can be done by using a foreach loop to iterate over the $_FILES array and accessing each file's properties such as name, type, size, and temporary location. By handling each file separately, you can properly validate, move, or process them as needed.
<?php
if(isset($_FILES['files'])){
$files = $_FILES['files'];
foreach($files['name'] as $key => $name){
$file_name = $files['name'][$key];
$file_tmp = $files['tmp_name'][$key];
$file_size = $files['size'][$key];
$file_type = $files['type'][$key];
// Process each file here (e.g. move_uploaded_file, validate file type/size, etc.)
}
}
?>