Multi-file upload in PHP

Asked

Viewed 886 times

1

I’m a beginner in PHP and I’m developing a real estate system. The problem that occurred to me was the following: I need to upload 20 photos of a property using only 1 input and save the name of the file in the bank. I can upload the files and resize them perfectly, but I’m having trouble saving the name of each image separately in the table. For example, I can interpret the image and rename it but the first image should be in the column "foto1" while the second in the column "foto2", but I did not find function to interpret each file separately, then all columns get the value only of the last selected file in the Multiple input.

Reduced version of the code:

 <html>
   <body>
     <form action="#" method="POST" enctype="multipart/form-data">
        <input type="file" name="fileUpload[]" multiple>
        <input type="submit" value="Enviar">
     </form>
  </body>
 </html>

  <?php
     if(isset($_FILES['fileUpload']))
     {
        require 'WideImage/WideImage.php'; 

        date_default_timezone_set("Brazil/East");

        $name   = $_FILES['fileUpload']['name']; 
        $tmp_name = $_FILES['fileUpload']['tmp_name']; 

        $allowedExts = array(".gif", ".jpeg", ".jpg", ".png", ".bmp"); 

        $dir = 'uploads/';

        for($i = 0; $i < count($tmp_name); $i++) 
        {
           $ext = strtolower(substr($name[$i],-4));

           if(in_array($ext, $allowedExts)) 
           {
              $new_name = date("Y.m.d-H.i.s") ."-". $i . $ext;

              $image = WideImage::load($tmp_name[$i]); 

              $image = $image->resize(170, 180, 'outside'); 

              $image = $image->crop('center', 'center', 170, 180); 

              $image->saveToFile($dir.$new_name); 

              $sql = "INSERT INTO imoveis (foto1) VALUES ('$new_name')";
              $result = mysql_query($sql) or die('Erro ao executar instrucao SQL :-( ERRO->'.mysql_error());
           }
        }
     }
  ?>
  • Post the code you have to help you better :)

  • Hello, you say you can upload the 20 photos in question and even rename, for this you must be using a LOOP to access the X photos, What you can do is insert the photos into the BD in the same LOOP as you upload the photos or save the photo path in an ARRAY and create a new LOOP to access this array and insert them into the BD. Why don’t you put the code in question?.

  • I added the code to the question Lucas!

  • With your tip I added INSERT INTO in the loop and managed to insert a value in the Lucas table! Now the question is: How to continue making the second photo in the photo2 column of the table and so on?

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.