[英]Uploading multiple images with dropzone.js with PHP
I am building a simple image upload with dropzone.js . 我正在使用dropzone.js构建一个简单的图像上传。 Everything is working when I upload a single image, however, when I try dropping and uploading multiple images at once the upload behaves strangely.
当我上传单个图像时,一切正常,但是,当我尝试一次删除并上传多个图像时,上传的行为异常。 Sometimes it uploads a couple images while ignoring the others.
有时,它会上传一些图像,而忽略其他图像。 And sometimes it doesn't upload any.
有时它不上传任何东西。 I was under the impression that dropzone sends a seperate post for each file when uploading multiple files according to its documentation so I can't figure out if this is a PHP issue or dropzone related.
我的印象是,当dropzone根据其文档上载多个文件时,dropzone为每个文件发送单独的帖子,所以我不知道这是PHP问题还是与dropzone相关。
Here is what I have so far: 这是我到目前为止的内容:
HTML: HTML:
<form id="dzone" action="upload.php" class="dropzone">
<p class="dz-message"><span>Drop images here or click to upload.</span><br />
<span class="note">Only images with the following file types are allowed <strong>(jpg , jpeg , png , bmp)</strong></span></p>
<!-- no-script fallback -->
<div class="fallback">
<input name="file" type="file" multiple />
</div>
</form>
JS: JS:
Dropzone.options.dzone = {
acceptedFiles: "image/*",
maxFilesize: 20,
parallelUploads: 3
};
PHP: PHP:
$ds = DIRECTORY_SEPARATOR;
$upload_location = 'uploads';
if(!empty($_FILES)) {
$temp_file = $_FILES['file']['tmp_name'];
// Check if uploaded file is an image
if(!is_valid_type($temp_file)) {
echo '<p>only images are allowed</p>';
exit;
}
$temp_name = explode('.' , $_FILES['file']['name']);
$target_path = dirname(__FILE__) . $ds . $upload_location . $ds;
$new_file_name = round(microtime(true)) . '.' . end($temp_name);
$target_file = $target_path . $new_file_name;
move_uploaded_file($temp_file , $target_file);
// Change file permission to read only
chmod($target_file, 0644);
}
function is_valid_type($file) {
// Image Size
$size = getimagesize($file);
if(!$size) {
return 0;
}
// Valid file types
$valid_types = array(IMAGETYPE_GIF , IMAGETYPE_JPEG , IMAGETYPE_PNG , IMAGETYPE_BMP);
if(in_array($size[2] , $valid_types)) {
return 1;
} else {
return 0;
}
}
I have tried altering the PHP form engine to process each file in a foreach loop, but it results with the same issue. 我尝试过更改PHP表单引擎以在foreach循环中处理每个文件,但结果相同。 Am I missing something?
我想念什么吗?
here is the correct code that solves your question! 这是解决您问题的正确代码!
Change this at your upload.php file: 在您的upload.php文件中更改此设置:
change string: 更改字符串:
round(microtime(true))
to: 至:
round(microtime(true)) + rand()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.