我使用jQuery-File-Upload并试图限制最大图片上传为1(单个图片上传)。如何在PHP中从后端jQuery文件上传限制最大1个文件上传
我使用例如PHP文件从演示版本身它位于server/php/
为UploadHandler.php
截至目前我已经封锁拖放多重图像上传的JS但由于其只是在客户端作为一个完整的证明解决方案是不可信的,我想知道如何限制最大数量。每个请求
上传直到现在我都做:
<input class="photo file" id="fileupload" name="images" type="file">
而且对JS部分:
$('#fileupload').fileupload({
url: url,
dataType: 'json',
autoUpload: true,
dropZone: $('#fileupload'),
singleFileUploads: false,
}).on('fileuploadadd', function (e, data) {
//Hides error
$('.error').hide().children().text('');
var maxFileUploadAllowed = 1;
var fileCount = data.files.length;
if (fileCount > maxFileUploadAllowed) {
$('.error').fadeIn().children().text("You are allowed to upload only 1 file");
return false;
}
,并可以正常工作,但限制在后台1个文件还需要
关于一些挖掘我发现options
有一个参数为'max_number_of_files' => null,
我试着改变它到1
但随后对每个文件上传即使在单个文件上传开始给误差Maximum File limit exceeded
而且经过进一步挖掘我发现,在这个if
语句检查是某种真实而造成的错误。
if (is_int($this->options['max_number_of_files']) &&
($this->count_file_objects() >= $this->options['max_number_of_files']) &&
// Ignore additional chunks of existing files:
!is_file($this->get_upload_path($file->name))) {
$file->error = $this->get_error_message('max_number_of_files');
return false;
}
我没有回音$this->count_file_objects()
,发现它实际上返回允许的上传目录的最大文件。
所以没有处理程序在演示代码,可以限制没有。的文件上传在后端。
只是想知道是否有办法我可以限制不。文件上传在后端?
会有问题,当用户将拨弄以下内容:
- 名称阵列
<input class="photo file" id="fileupload" name="images[]" type="file">
- “多” 属性中
<input class="photo file" id="fileupload" name="images[]" type="file" multiple>
- 或者拨弄着下面的一段
代码如下:
var maxFileUploadAllowed = 1;
var fileCount = data.files.length;
if (fileCount > maxFileUploadAllowed) {
$('.error').fadeIn().children().text("You are allowed to upload only 1 file");
return false;
}
由于在后端没有检查。