Input type File exceeds bytes in PHP

1

If I upload a small audio file (15sec) it works fine, but when I do it with a song (4min) it bursts:

Warning: POST Content-Length of 9042845 bytes exceeds the limit of 8388608 bytes in Unknown on line 0

In the view:

     
<h2>Agregar Categoría</h2> 
<hr/>
<div class="container">
    <form action="AgregarCategoria" method="POST" enctype="multipart/form-data">
        {!! csrf_field() !!}
        <div class="form-group" style="margin-top: 20px;"> 
            <label for="Nombre Categoría">Nombre Categoría:</label>
            <input type="text" placeholder="Nombre de la categoría" name="txtcategoria" id='idtxtcategoria' class="form-control" required>
        </div>    
        <div class="form-group">
            <label for="Imagen Categoría">Imagen/Pictograma:</label><br>
            <input type="file" placeholder="Subir la Imagen" name="fileImg"  id='idfileimg' class="form-control"  accept="image/png, .jpeg, .jpg, image/gif"  required>
        </div>
        <div class="form-group">
            <label for="Audio Categoría">Sonido/Audio:</label><br>
            <input type="hidden" name="MAX_FILE_SIZE" value="100000" />
            <input type="file" placeholder="Audio" name="fileAudio"   id='idfileaudio'  class="form-control"  required accept=".mp3,audio/*">
        </div>    
        <div class="form-group" >
            <label for="colorin">Elige el color de la categoría:</label><br>
            <input name="color" type="color" id="colorin" value=""/>
        </div>
        <div class="form-group" >    
            <input type="submit" class="btn btn-primary" name="btnEnviarCategoria" value="Agregar">
            <input type="button" name="volver" value="Limpiar" onclick="LimpiarDatos();" class="btn btn-primary"><br><br>
        </div>
    </form>
</div>
<!-- Fin Agregar Categoría --> 

On the controller:

 public function AgregarCategoria(Request $req) {

    $conexion = new Conexion();

    $NombreCategoria = $req->get('txtcategoria');
    $img = $req->file('fileImg');
    $audio = $req->file('fileAudio');

    $ruta_img = $img->getClientOriginalName();
    $ruta_audio = $audio->getClientOriginalName();

    \Storage::disk('local')->put('categorias/pictogramas/' . $ruta_img, \File::get($img));
    \Storage::disk('local')->put('categorias/audios/' . $ruta_audio, \File::get($audio));

    $color = $req->get('color');

    $conexion->insertar_categoria($NombreCategoria, $this->CarpetaCategoria . $this->CarpetaCategoriaPictograma . $ruta_img, $this->CarpetaCategoria . $this->CarpetaCategoriaAudios . $ruta_audio, $color);

    $this->Cerrar_Conexion($conexion);

    return view('admin/administrador');
}

Upload it, upload it well, but if they are heavy files, bust the application and I have put <input type="hidden" name="MAX_FILE_SIZE" value="100000" /> before the file field and nothing.

    
asked by EduBw 28.02.2018 в 23:58
source

2 answers

4

The problem

The message:

  

POST Content-Length of XXXXXXX bytes exceeds the limit of 8388608   bytes

occurs when you try to send by POST a file whose size exceeds the maximum size allowed for a file that must be sent by POST , set to 8MB or 8388608 bytes, according to the PHP Manual.

Solution

Change the value of post_max_size to a larger value, but taking into account what the PHP Manual says with respect to the value that other variables should have related to post_max_size :

  

post_max_size defines the maximum size of POST data allowed. This option also affects the upload of files. For   upload large files, this value must be greater than    upload_max_filesize . As a general rule, memory_limit must be   greater than post_max_size . When an integer is used, the value of the   same is measured in bytes. You can also use the reduced notation,   as described in this FAQ .

     

If the POST data size is greater than post_max_size , the   superglobal $ _POST and $_FILES will be empty. This can be   track in several ways, for example, passing the variable $_GET   to the script that processes the data, that is, <form action="edit.php?procesado=1"> , and then check if the variable    $_GET['procesado'] exists.

How to configure it?

It can be done through php.ini and in some cases through .htaccess . At least when done by php.ini it is necessary to restart the server for the changes to take effect.

Example using php.ini :

1.

It would be nice if you open php.ini , look for the value post_max_size , if you find it just change the value it has for a larger one. If the line starts with ; , you delete the ; .

; Maximum size of POST data that PHP will accept.
; Its value may be 0 to disable the limit. It is ignored if POST data reading
; is disabled through enable_post_data_reading.
; http://php.net/post-max-size
post_max_size = 64M

2.

As stated in the manual note above, verify that the value of upload_max_filesize is not greater than the value you set for post_max_size . You should have something like this more or less in php.ini

; Maximum allowed size for uploaded files.
; http://php.net/upload-max-filesize
upload_max_filesize = 64M

3.

Verify that the value of memory_limit is not less than the value set for post_max_size . You should have something like this more or less:

; Maximum amount of memory a script may consume (128MB)
; http://php.net/memory-limit
memory_limit = 256M

4.

Do an operation GRI :):

  • Save your php.ini ,
  • Restart the server
  • Try again to upload the file ...

It should work.

    
answered by 01.03.2018 / 00:35
source
2

To limit the file upload size, you can use the same fields that the form gives you to determine the size, I'm talking in the PHP part:

$audio = $_FILES['file']['name'];
$size = $_FILES['file']['size'];
$type = $_FILES['file']['type'];

Then you just have to do something like:

if(($size <= 0) || ($size > 100000 )) {
    // echo "Fichero muy grande"
    exit();
} else {
    // Subir fichero, guardar y demás acciones
}
    
answered by 01.03.2018 в 09:28