简体   繁体   中英

Multiple Image Upload using PHP into MYSQL database

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM