Compares size of the selected files in the input with the PHP disk_free_space

0

I need a javascript script to compare the selected files size in input with disk_free_space (in my case disk_free_space('D:') ") of PHP , but it has to be before sending to the page that does I'm using upload of multiple files and I have the AJAX available.

>

<form id="formFiles" name="formFiles" action="javascript:void(0);" enctype="multipart/form-data">
  <input type="file" required name="arquivo[]" multiple="multiple">
  <button id="" type="submit">Enviar</button>
</form>
    
asked by anonymous 19.08.2018 / 00:48

1 answer

1
___ erkimt ___ Compares size of the selected files in the input with the PHP disk_free_space ______ qstntxt ___

I need a javascript script to compare the selected files size in element.files with $("input").prop("files") (in my case %code% ") of %code% , but it has to be before sending to the page that does I'm using upload of multiple files and I have the %code% available.

>

/* Como o aqui não irá funcionar o código PHP, deixarei comentado */
//const MAX_FILESIZE = parseInt("<?php echo disk_free_space('D:'); ?>")

/* Exemplo do valor retornado no código anterior */
const MAX_FILESIZE = 2000000

$("input:file").change(function() {
  
  /* Reinicia o tamanho acumulado */
  let size = 0
  
  /* Captura o metada dos arquivos escolhidos e percorrer todos eles */
  Array.from($(this).prop("files")).map( file => {
    
    /* Soma o tamanho do arquivo em bytes com o tamanho acumulado */
    size += file.size
  })
  
  /**
   * Habilita o botão caso 'size' seja menor que 'MAX_FILESIZE',
   * caso contrário desabilita
   */
  $("button:submit").prop("disabled", (size > MAX_FILESIZE))
})
    
______ ___ azszpr323425

It is possible to do so. Just use %code% or %code% , with this you will have access to the metadata file (size, mimetype etc.).

Commented example:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><formid="formFiles" name="formFiles" action="javascript:void(0);" enctype="multipart/form-data">
  <input type="file" required name="arquivo[]" multiple="multiple">
  <button id="" type="submit">Enviar</button>
</form>
/* Como o aqui não irá funcionar o código PHP, deixarei comentado */
//const MAX_FILESIZE = parseInt("<?php echo disk_free_space('D:'); ?>")

/* Exemplo do valor retornado no código anterior */
const MAX_FILESIZE = 2000000

$("input:file").change(function() {
  
  /* Reinicia o tamanho acumulado */
  let size = 0
  
  /* Captura o metada dos arquivos escolhidos e percorrer todos eles */
  Array.from($(this).prop("files")).map( file => {
    
    /* Soma o tamanho do arquivo em bytes com o tamanho acumulado */
    size += file.size
  })
  
  /**
   * Habilita o botão caso 'size' seja menor que 'MAX_FILESIZE',
   * caso contrário desabilita
   */
  $("button:submit").prop("disabled", (size > MAX_FILESIZE))
})
    
___
19.08.2018 / 07:32