2016-11-06 107 views
0

我使用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(),发现它实际上返回允许的上传目录的最大文件。

所以没有处理程序在演示代码,可以限制没有。的文件上传在后端。

只是想知道是否有办法我可以限制不。文件上传在后端?

会有问题,当用户将拨弄以下内容:

  1. 名称阵列<input class="photo file" id="fileupload" name="images[]" type="file">
  2. “多” 属性中<input class="photo file" id="fileupload" name="images[]" type="file" multiple>
  3. 或者拨弄着下面的一段

代码如下:

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; 
    } 

由于在后端没有检查。

回答

0

server/php/UploadHandler.php

更深的挖掘中演示的PHP代码后,我发现上传过程正在POST从而$this->post($this->options['print_response']);方法被调用来处理上传:

而这个过程之前,也有一些在validate()初步验证:

protected function validate($uploaded_file, $file, $error, $index) { 
    if ($error) { 
     $file->error = $this->get_error_message($error); 
     return false; 
    } 
    $content_length = $this->fix_integer_overflow(
     (int)$this->get_server_var('CONTENT_LENGTH') 
    ); 
    $post_max_size = $this->get_config_bytes(ini_get('post_max_size')); 
    if ($post_max_size && ($content_length > $post_max_size)) { 
     $file->error = $this->get_error_message('post_max_size'); 
     return false; 
    } 
... 
return true; 
} 

所以很显然,validate()负责初步检查。

最初,在选项中,我添加了一个名为max_files_upload_allowed: 1参数,然后我在$error_messages'max_files_upload_allowed' => 'You are not allowed to upload multiple files at once'

增加了错误信息,那么我创造了我的方法getUploadFilesCount()检查没有。文件的用户添加到上传队列:

protected function getUploadFilesCount() { 
     $file_upload_count = $this->get_upload_data($this->options['param_name']); 
     return count($file_upload_count['name']); 
    } 

而在这之后添加下列检查中validate()

//Check max_files_upload_allowed here 
if (is_int($this->options['max_files_upload_allowed']) && (
     $this->getUploadFilesCount() > $this->options['max_files_upload_allowed'] && 
     $this->options['max_files_upload_allowed'] > 0) 
    ) { 
      $file->error = $this->get_error_message('max_files_upload_allowed'); 
    return false; 
} 

瞧!如果用户添加的文件多于max_files_upload_allowed: 1中列出的文件,则会收到错误。

而且解决了这个目的,防止恶意尝试并为使用的文件上传过程增加更多可靠性。