title: How to limit maximum 1 file upload in jQuery-File-Upload from backend in PHP
body: <p>I am using <a href="https://github.com/blueimp/jQuery-File-Upload" rel="nofollow noreferrer">jQuery-File-Upload</a> and trying to limit the maximum image uploads to 1 (single image upload).</p>
  
  <p>I am using example php file from the demo version itself which is located in <code>server/php/</code> as <a href="https://github.com/blueimp/jQuery-File-Upload/blob/master/server/php/UploadHandler.php" rel="nofollow noreferrer">UploadHandler.php</a> </p>
  
  <p>As of now I have blocked the multiple image uploads on drag and drop in JS but as its just on the client side and which can't be trusted as a full proof solution, I want to know how can I limit the maximum no. of uploads per request</p>
  
  <p>Till now I have done: </p>
  
  <p><code>&lt;input class="photo file" id="fileupload" name="images" type="file"&gt;</code></p>
  
  <p>And on JS part:</p>
  
  <pre><code>$('#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 &gt; maxFileUploadAllowed) {
        $('.error').fadeIn().children().text("You are allowed to upload only 1 file");
        return false;
      }
  </code></pre>
  
  <p>and that works properly, but limiting 1 file on backend is also necessary</p>
  
  <p>On some digging I found that there's a parameter in <code>options</code> as <code>'max_number_of_files' =&gt; null,</code> I tried changing it to <code>1</code> but then on each file upload even on single file upload it started giving error as <code>Maximum File limit exceeded</code></p>
  
  <p>And after further digging I found that checks in this <code>if</code> statement is somehow true and causing error.</p>
  
  <pre><code>if (is_int($this-&gt;options['max_number_of_files']) &amp;&amp;
              ($this-&gt;count_file_objects() &gt;= $this-&gt;options['max_number_of_files']) &amp;&amp;
              // Ignore additional chunks of existing files:
              !is_file($this-&gt;get_upload_path($file-&gt;name))) {
          $file-&gt;error = $this-&gt;get_error_message('max_number_of_files');
          return false;
      }
  </code></pre>
  
  <p>I did echo <code>$this-&gt;count_file_objects()</code> and found that it's actually returning the maximum files allowed in the uploads directory.</p>
  
  <p>So there's no handler in demo code which can limit no. of file uploads on the backend.</p>
  
  <p>Just wanted to know if there's a way I can restrict the no. of file uploads in the backend?</p>
  
  <p>There will be issues when user will fiddle the following:</p>
  
  <ol>
  <li>Name array <code>&lt;input class="photo file" id="fileupload" name="images[]" type="file"&gt;</code></li>
  <li>"multiple" attribute in <code>&lt;input class="photo file" id="fileupload" name="images[]" type="file" multiple&gt;</code></li>
  <li>Or fiddle with the following piece of </li>
  </ol>
  
  <p>code below:</p>
  
  <pre><code>var maxFileUploadAllowed = 1;
      var fileCount = data.files.length;
      if (fileCount &gt; maxFileUploadAllowed) {
        $('.error').fadeIn().children().text("You are allowed to upload only 1 file");
        return false;
      }
  </code></pre>
  
  <p>As there's no check in the backend.</p>