How to upload multiple files with this code?

1

How can I make this code can select multiple files and print the names of the files (Incidentally the code adds digits to the file name)

if(isset($_FILES['file'])){

//imagen uno  
  $file = $_FILES['file'];
  // file properties
  $file_name = $file['name'];
  $file_tmp = $file['tmp_name'];
  $file_size = $file['size'];
  $file_error = $file['error'];

  //work out the file extension
  $file_ext = explode('.', $file_name);
  $file_ext = strtolower(end($file_ext));

  $allowed = array('txt', 'jpg');

  if(in_array($file_ext, $allowed)){
    if($file_error === 0){
      if($file_size <= 2097152){

        $file_name_new = uniqid('', true) . '.' . $file_ext;
        $file_destination = 'images/radiografia/' . $file_name_new;

        if(move_uploaded_file($file_tmp, $file_destination)){
          echo $file_destination;
        }

      }
    }
  }

        <input type="file" name="file">
    
asked by RAIC3R 01.10.2018 в 05:32
source

1 answer

1

I currently use this code, maybe it will help you!

<form role="form" method="POST" action="addphoto.php<?php echo '?id='.$pid; ?>" enctype="multipart/form-data">

                              <div style="height:10px;"></div>                  
                                <div class="form-group input-group">
                                <span class="input-group-addon" style="width:120px;">Photo:</span> 
                                <input type="file" style="width:400px;" class="form-control" name="archivo[]" multiple="" accept="image/*" /> //aquí se declara archivo como vector para recibir varias imagenes.
                                </div>


                         <div class="col-sm-10">




                        <?php 

                                   $sql = "select * from carousel as c where c.productid = '$pid'"; 
                                    $result = mysqli_query($conn, $sql);



                                        if(mysqli_num_rows($result) > 0){
                                        while($row = mysqli_fetch_array($result)){
                                        $id = $row['idphoto'];
                                        $photo = $row['photo'];

                                              for($i=0; $i < count($photo); $i++){




                                        echo "<img src='$photo' width=200px  />";

                                       echo " 

                                         <a class='btn btn-danger glyphicon glyphicon-remove' href='del_file.php/?service=$id&service2=$photo'>Borrar</a>

                                       ";                                    } 

                                    }

                                    }



                                   ?>





                    </div>                     





                       <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal"><i class="fa fa-times"></i> Cancel</button>
                    <button type="submit" class="btn btn-success"><i class="fa fa-check-square-o"></i> Add Photo</button>
                    </form>

this is the addphoto.php

<?php
    include('session.php');

    $id=$_GET['id'];

    //Como el elemento es un arreglos utilizamos foreach para extraer todos los valores
    foreach($_FILES["archivo"]['tmp_name'] as $key => $tmp_name)
    {
        //Validamos que el archivo exista
        if($_FILES["archivo"]["name"][$key]) {
            $filename = $_FILES["archivo"]["name"][$key]; //Obtenemos el nombre original del archivo
            $source = $_FILES["archivo"]["tmp_name"][$key]; //Obtenemos un nombre temporal del archivo

            $directorio = 'upload'; //Declaramos un  variable con la ruta donde guardaremos los archivos

            //Validamos si la ruta de destino existe, en caso de no existir la creamos
            if(!file_exists($directorio)){
                mkdir($directorio, 0777) or die("No se puede crear el directorio de extracci&oacute;n");    
            }

            $dir=opendir($directorio); //Abrimos el directorio de destino
            $target_path = $directorio.'/'.$filename; //Indicamos la ruta de destino, así como el nombre del archivo

            //Movemos y validamos que el archivo se haya cargado correctamente
            //El primer campo es el origen y el segundo el destino
            if(move_uploaded_file($source, $target_path)) { 
                echo "El archivo $filename se ha almacenado en forma exitosa.<br>";
                } else {    
                echo "Ha ocurrido un error, por favor inténtelo de nuevo.<br>";
            }
            closedir($dir); //Cerramos el directorio de destino


             mysqli_query($conn,"call GuardarImagen('$id','$target_path')"); 
             $pid=mysqli_insert_id($conn);


        }
    }
    ?>
    <script>
            window.alert('Product added successfully!');
            window.history.back();
        </script>

?>
    
answered by 01.10.2018 / 06:25
source