2016-11-06 106 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中列出的文件,則會收到錯誤。

而且解決了這個目的,防止惡意嘗試併爲使用的文件上傳過程增加更多可靠性。