2016-11-06 9 views
0

jQuery-File-Upload을 사용하고 최대 이미지 업로드를 1 (단일 이미지 업로드)로 제한하려고합니다.PHP의 백엔드에서 jQuery 파일 업로드시 최대 파일 1 개를 제한하는 방법

나는하지만 단지 클라이언트 측에서 같은 JS에서 드래그 앤 드롭에 여러 이미지 업로드를 차단 한 현재로서는 UploadHandler.php

server/php/에있는 데모 버전 자체에서 예를 들어, PHP 파일을 사용하고 있습니다 전체 증명 솔루션으로 신뢰할 수없는 경우 최대 허용 수를 어떻게 제한 할 수 있는지 알고 싶습니다. 지금까지 요청

당 업로드의 내가 행한 :

<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>에 아래 :

    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라는 매개 변수를 추가하고 난 후, 나는 전혀 확인하는 내 방법 getUploadFilesCount()을 만든 $error_messages'max_files_upload_allowed' => 'You are not allowed to upload multiple files at once'

    에 오류 메시지를 추가했습니다. 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; 
    } 
    

    그리고 짜잔 체크인 다음

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

    그리고 그 후

    추가 : 파일의 사용자가 큐를 업로드에 추가! 사용자가 max_files_upload_allowed: 1에 나열된 것보다 많은 파일을 추가하면 오류가 발생합니다.

    그리고 그 목적을 해결하고 악의적 인 시도를 방지하며 사용되는 파일 업로드 프로세스에 더 많은 안정성을 부여합니다.