我使用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;
}
由於在後端沒有檢查。