How to upload multiple images to the server in a specific folder with PHP? [closed]

-1

I want from a form to be able to upload several images at the time, that is sent by post, that in upload.php this form and that in process.php is sent the form in such a way that it can be stored in a specific folder.

    
asked by Stivents 01.11.2016 в 20:05
source

1 answer

2

Here is an example to your answer.

Page subir.php

<form method="post" action="proceso.php" enctype="multipart/form-data">
    Subir imagen: <input type="file" name="file[]" multiple>
    <input type="submit" value="Subir imágenes" />
 </form>

Page proceso.php

<?php

if (isset($_FILES["file"]))
{
   $reporte = null;
     for($x=0; $x<count($_FILES["file"]["name"]); $x++)
    {
      $file = $_FILES["file"];
      $nombre = $file["name"][$x];
      $tipo = $file["type"][$x];
      $ruta_provisional = $file["tmp_name"][$x];
      $size = $file["size"][$x];
      $dimensiones = getimagesize($ruta_provisional);
      $width = $dimensiones[0];
      $height = $dimensiones[1];
      $carpeta = "tu_ruta/";

      if ($tipo != 'image/jpeg' && $tipo != 'image/jpg' && $tipo != 'image/png' && $tipo != 'image/gif')
      {
          $reporte .= "<p style='color: red'>Error $nombre, el archivo no es una imagen.</p>";
      }
      else if($size > 1024*1024)
      {
          $reporte .= "<p style='color: red'>Error $nombre, el tamaño máximo permitido es 1mb</p>";
      }
      else if($width > 500 || $height > 500)
      {
          $reporte .= "<p style='color: red'>Error $nombre, la anchura y la altura máxima permitida es de 500px</p>";
      }
      else if($width < 60 || $height < 60)
      {
          $reporte .= "<p style='color: red'>Error $nombre, la anchura y la altura mínima permitida es de 60px</p>";
      }
      else
      {
          $src = $carpeta.$nombre;

          //Caragamos imagenes al servidor
          move_uploaded_file($ruta_provisional, $src);       

          //Codigo para insertar imagenes a tu Base de datos.
          //Sentencia SQL

          echo "<p style='color: blue'>La imagen $nombre ha sido subida con éxito</p>";
      }
    }

    echo $reporte;
}
    
answered by 01.11.2016 / 21:09
source