0

I am currently very puzzled over why I am unable to insert an array of images over into my database. As of now, my current multiple file upload is able to upload images into my default directory, and able to store ONLY the first image into my SQL server database, why is this so? Shouldn't the foreach command be able to split all the multiple file that i upload and store them respectively into the database? Please shed some lights on this, thank you!

HTML Code

<form method="post" enctype="multipart/form-data"  action="">  
    <input type="file" name="files[]" id="files" multiple />  
    <br /><br />
    <button type="submit">Upload selected files</button>  

PHP Code

foreach ($_FILES["files"]["error"] as $key => $error) {
        if ($error == UPLOAD_ERR_OK) {
            $name = $_FILES["files"]["name"][$key];
            move_uploaded_file($_FILES["files"]["tmp_name"][$key], "" . $_FILES['files']['name'][$key]);
            $sql = "INSERT INTO `test`(`image`) VALUES ('" . $name . "')";
            $result = mysqli_query($connection, $sql);
            echo "The file " . basename($_FILES['multiple_uploaded_files']['name']) . " has been uploaded";
        } else {
            echo "There was an error uploading the file, please try again!";
        }
    }

Cheers, A tech newbie learning in progress.

Newbie
  • 145
  • 2
  • 7
  • 23
  • I recommend reading this: http://stackoverflow.com/questions/6472233/can-i-store-images-in-mysql – Som1 Nov 05 '14 at 16:15
  • Are you saying that ALL the uploaded images get uploaded and moved to a proper location, but only the name of the first one gets stored into the db? – Shomz Nov 05 '14 at 16:15
  • I've tried it on localhost, and it should work, i do not see any error in your code. Only this line: `$_FILES['multiple_uploaded_files']` There are no key in $_FILES like this. It should be: `basename($_FILES['files']['name'][$key])` – vaso123 Nov 05 '14 at 16:18
  • When using `mysqli` you should be using parameterized queries and [`bind_param`](http://php.net/manual/en/mysqli-stmt.bind-param.php) to add user data to your query. **DO NOT** use string concatenation to accomplish this because you will create severe [SQL injection bugs](http://bobby-tables.com/). – tadman Nov 05 '14 at 16:38
  • To : Shomz, to answer your Q, yes! I dont understand why too To : lolka_bolka, yes I also do not understand why it is not working, by right it should :) – Newbie Nov 05 '14 at 16:57

1 Answers1

1
try with this example code,

    $path = "imageuploads/";
     for($i=0; $i<count($_FILES['file']['name']); $i++){
     $extension = explode('.', basename( $_FILES['file']['name'][$i]));
     $path = $path . md5(uniqid()) . "." . $extension[count($extension)-1]; 

      if(move_uploaded_file($_FILES['file']['tmp_name'][$i], $path )) {
      //insert query 
         echo "uploaded successfully";
          } else{
        echo "Error in Upload";
       }
   }
Above code is not worked, please tell the scenario
  • Hi Saleem, thank you for your help. There's a problem with the execution of the code, the files are being moved to the directory as per code, problem 1: the file name is being stacked on top of each other, file1 name: 123.jpg file2 name: 123.jpg456.jpg. problem 2: none of the file being stored in the folder are being inserted into the database this time, thank you for the time and effort. :) – Newbie Nov 05 '14 at 17:08
  • 1
    hi Newbie Sorry was in chat room please, tell your problem – techy-coder Nov 05 '14 at 17:54
  • saleem its in the comment i posted just now, thanks! – Newbie Nov 06 '14 at 01:15