Intentions when uploading some images to the server with PHP

2

SOLVED THE PROBLEM.

The problem is already solved and I explain it to you next in case someone else happens the same.

The code initially worked without any problem but when uploading the images some of them failed and were uploaded in a corrupted way or upload to the server.

After trying a thousand ways to upload the files I found the fault and it was not in the code but in the configuration of xampp / wampp. These default programs have a memory limit configured to use in the forms that is usually quite low in my case it was 2M what I did is increase it to 12GB and problem solved !!

The steps I followed are the following: (with xampp)

  • Access the xampp directory
  • Enter php and search php.ini
  • Edit with the notepad or another editor and look for the following lines of code:

    memory_limit (al ser un server dedicado le puse el 80% de la RAM disponible.)
    upload_max (estaba en 2M y lo puse en 12000M unos 11GB)
    post_max_size(estaba en 2M y lo puse en 12000M unos 11GB)
    
  • The first parameter is the total php limit to work in memory, so it always has to be equal to or greater than the other two, the second is the maximum memory that it uses when uploading files (which is where it resided) my problem) and the third field is to send the complete form with the POST method.

    I hope it will help you because it is a very common problem and I have not found anything related in forums on how to solve it, luck to others!

    ## ERROR UPLOAD IMAGES HAVING THE CODE OK, BOTH THE BBDD AND THE SERVER ##

    I've been facing a curious error for a few weeks that I can not see solved in any Internet forum.

    I have a website where images are uploaded through a fairly simple code and a simple form, most images are uploaded without any problem and without any incidence. But there are certain images (I can not see that they have in common) that fail at the time of uploading to the server.

    I have tried everything that has occurred to me I have tried to store them both in the DB and move them to a folder in the server but the error persists with those images and I can not find out why it is ... I leave the code of the. php

    <?php
    SESSION_START();
    error_reporting(-1);
    $nom = $_SESSION['nombreusuario'];
    $conexion= mysqli_connect("localhost","root","","elarbusto");
    
    
    $caracteres='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
    $longpalabra=8;
    for($pass123='', $n=strlen($caracteres)-1; strlen($pass123) < $longpalabra ; ) {
      $x = rand(0,$n);
      $pass123.= $caracteres[$x];
    }
    
    $ruta="./ImagenesWEB/AvatarPerfil/";//ruta carpeta donde queremos copiar las imágenes 
    $uploadfile_temporal=$_FILES['Logo']['tmp_name']; 
    $uploadfile_nombre=$ruta.$_FILES['Logo']['name']; 
    
    $extension = ".".end(explode(".", $_FILES['Logo']['name']));
    $nombre = $nom.date('Y-m-d').$pass123.$extension;
    $nombreComp = $ruta.$nombre;
    
    
    if (is_uploaded_file($uploadfile_temporal)) 
    { 
        move_uploaded_file($uploadfile_temporal,$nombreComp); 
        $modusuarioLider = "UPDATE 'usuarios' SET 'Avatar' = '.$nombreComp' WHERE 'Nick' = '$nom' ";
        $realizarmod = mysqli_query($conexion, $modusuarioLider);   
        header("location:cuerpoweb/INDPerfil.php");
    
    
    } 
    else 
    { 
    header("location:cuerpoweb/INDPerfil.php?error=4");
    } 
    
    
    
    
    
    mysqli_close($conexion);
    ?>
    

    If someone sent from heaven is able to help me, I would be grateful enough!

    ADDED CONTENT TO CLARIFY MORE THEME:

    At the request of friend Oscar I upload a simpler version of the code, both an HTML and a php:

    upload picture folder.html:               Untitled document          

    <body>
    <form action="subirimagencarpeta2.php" method="post" enctype="multipart/form-data"> 
        Archivo: <input name="fichero" type="file"> 
        <input name="submit" type="submit" value="Upload!">  
    </form> 
    </body>
    </html> 
    

    upload picture folder2.php:

    <?php 
    $ruta="./pruebaimagenes/";//ruta carpeta donde queremos copiar las imágenes 
    $uploadfile_temporal=$_FILES['fichero']['tmp_name']; 
    $uploadfile_nombre=$ruta.$_FILES['fichero']['name']; 
    
    $extension = ".".end(explode(".", $_FILES['fichero']['name']));
    
    
    if (is_uploaded_file($uploadfile_temporal)) 
    { 
        move_uploaded_file($uploadfile_temporal,$ruta.'ejemplonombre'.$extension); 
    } 
    else 
    { 
    echo "error"; 
    } 
    
    
    
    ?> 
    <html> 
    <head> 
    <title>Documento sin t&iacute;tulo</title> 
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> 
    </head> 
    
    <body> 
    
        <?php  echo "<img src=".$ruta.'ejemplonombre'.$extension.">";  ?>
    
    </body> 
    </html>
    

    I think if you let me attach images, here is one that does not allow upload and another that does. (do not let me upload the images by size, I'll leave them in a link)

    Image that does not allow upload: link

    Image that allows upload: link

        
    asked by Guillermo 04.08.2018 в 19:04
    source

    1 answer

    0

    Basically my conclusion is that the image is corrupted for some reason.

    Any image by passing it to the getimagesize function to get its dimensions and other information returns an array .

    Example of the image that goes up:

    array (size=3)
      'filesize' => int 154007
      'filename' => string '74.png' (length=6)
      'size' => 
        array (size=6)
          0 => int 512
          1 => int 512
          2 => int 3
          3 => string 'width="512" height="512"' (length=24)
          'bits' => int 8
          'mime' => string 'image/png' (length=9)
    

    In contrast, the image that does not upload returns the following information:

    array (size=3)
      'filesize' => int 0
      'filename' => string 'imagen.png' (length=10)
      'size' => boolean false
    

    In addition to the corresponding Warning:

      

    Warning: getimagesize (): Filename can not be empty in / var / www / phptest / httpdocs

    This brings us another added problem, as we can not get the dimensions we can not edit it because we do not know what proportions to give it.

    So before reaching the conditional is_uploaded_file it's better to spend the image by getimagesize and check that it contains the minimum data to be able to treat it.
    ( Personal opinion ) Otherwise, discard it and inform the user that the image can not be uploaded Even add indications of possible solutions, such as what we have talked about that we know if it works, which is to first pass it through an image editor and save it again.

        
    answered by 04.08.2018 / 20:29
    source