What are some potential pitfalls when converting a single-file upload script to a multi-file upload script in PHP?
One potential pitfall when converting a single-file upload script to a multi-file upload script in PHP is properly handling multiple file inputs in the HTML form and processing them in the PHP script. To solve this issue, you can use the `name="file[]"` attribute in the HTML input element to create an array of file inputs, and loop through each file in the PHP script to handle the upload process for each file.
// HTML form with multiple file inputs
<form method="post" enctype="multipart/form-data">
<input type="file" name="files[]" multiple>
<input type="submit" name="submit" value="Upload">
</form>
// PHP script to handle multi-file upload
if(isset($_POST['submit'])){
$files = $_FILES['files'];
foreach($files['tmp_name'] as $key => $tmp_name){
$file_name = $files['name'][$key];
$file_tmp = $files['tmp_name'][$key];
$file_type = $files['type'][$key];
$file_size = $files['size'][$key];
// Process and move each file as needed
move_uploaded_file($file_tmp, "uploads/" . $file_name);
}
}