Join the Stack Overflow Community
Stack Overflow is a community of 6.5 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

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.

share|improve this question
    
I recommend reading this: stackoverflow.com/questions/6472233/can-i-store-images-in-my‌​sql – Som1 Nov 5 '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 5 '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]) – karacsi_maci Nov 5 '14 at 16:18
    
When using mysqli you should be using parameterized queries and bind_param to add user data to your query. DO NOT use string concatenation to accomplish this because you will create severe SQL injection bugs. – tadman Nov 5 '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 5 '14 at 16:57
up vote 1 down vote accepted
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
share|improve this answer
    
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 5 '14 at 17:08
    
@Newbie Welcome Dude! Cool. – techy-coder Nov 5 '14 at 17:12
    
Uh, there's 2 problems over there lol. Please help :) – Newbie Nov 5 '14 at 17:34
1  
hi Newbie Sorry was in chat room please, tell your problem – techy-coder Nov 5 '14 at 17:54
    
saleem its in the comment i posted just now, thanks! – Newbie Nov 6 '14 at 1:15

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.