Dropzone开启uploadMultiple=true时服务端上传脚本失效问题
uploadMultiple Not Working with PHP Backend Hey, let's sort out this Dropzone upload issue you're facing! The problem boils down to how Dropzone sends files when uploadMultiple is enabled, and how your PHP script is trying to read them.
The Root Cause
When you set uploadMultiple: true, Dropzone sends files as an array instead of a single file. That means the form field name becomes file[] (with square brackets) instead of just file. Your original PHP script is looking for $_FILES['file']—which expects a single file structure—so it can't pick up the uploaded files correctly.
Step 1: Tweak Your Frontend (Optional but Smoother)
Your existing JavaScript is mostly fine, but adding parallelUploads helps control how many files upload at once, which plays nicely with uploadMultiple:
var myDropzone = new Dropzone("div#myDropzone", { url: "/tools/news/uploadimage.php", uploadMultiple: true, parallelUploads: 3 // Adjust this number based on your needs }); myDropzone.on("queuecomplete", function() { alert("All files have uploaded!"); // closeJustPopup('uploadImagePopup'); });
Step 2: Fix the PHP Backend
You need to update your script to handle the file array. Here's the revised version with key improvements:
<?php $ds = DIRECTORY_SEPARATOR; $storeFolder = 'images'; // Make sure the storage folder exists (prevents upload failures from missing dir) if (!is_dir($storeFolder)) { mkdir($storeFolder, 0755, true); } if (!empty($_FILES['file'])) { // Loop through each uploaded file in the array foreach ($_FILES['file']['tmp_name'] as $key => $tmpFile) { $originalName = $_FILES['file']['name'][$key]; $uploadError = $_FILES['file']['error'][$key]; // Only process files that uploaded successfully if ($uploadError === UPLOAD_ERR_OK) { $extension = pathinfo($originalName, PATHINFO_EXTENSION); // Generate a unique filename to avoid overwriting existing files $targetFile = dirname(__FILE__) . $ds . $storeFolder . $ds . date('YmdHis') . '_' . $key . '.' . $extension; move_uploaded_file($tmpFile, $targetFile); } } } ?>
Key Fixes in the PHP Script:
- Uses
foreachto loop through the$_FILES['file']array instead of treating it as a single file - Adds a check to create the storage folder if it doesn't exist (no more "file not found" errors)
- Appends
$keyto the filename to prevent overwriting files uploaded at the same time - Checks
$uploadErrorto ensure we only process files that uploaded without issues
With these changes, your Dropzone multi-file upload should work perfectly with your PHP backend!
内容的提问来源于stack exchange,提问作者Ed Mozley

