-1

I want to upload multiple images in MySql database in the same row - separated by a comma(,).

For uploading and processing the images I am using the DropZoneJS javascript library.

The PHP code of dropzone library for moving the images to the propertyimage folder:

Also, the below code is saved in a file let's say upload-dropzone-img.php and the code to upload the image on MySQL is written in other file called function.php

$folder_name = 'propertyimage/';

if(!empty($_FILES))
{
 global $con;
 $temp_file = $_FILES['file']['tmp_name'];
 $location = $folder_name . $_FILES['file']['name'];
 move_uploaded_file($temp_file, $location);

}

if(isset($_POST["name"]))
{
 $filename = $folder_name.$_POST["name"];
 unlink($filename);
}

$result = array();

$files = scandir('propertyimage');

$output = '<div class="row">';

if(false !== $files)
{
 foreach($files as $file)
 {
  if('.' !=  $file && '..' != $file)
  {
   $output .= '
   <div class="col-md-2">
    <img src="'.$folder_name.$file.'" class="img-thumbnail" width="175" height="175" style="height:175px;" />
    <button type="button" class="btn btn-link remove_image" id="'.$file.'">Remove</button>
   </div>
   ';
  }
 }
}
$output .= '</div>';

I want to store the $location variable data into the MySql database with comma-separated.

My HTML code :

<form method='POST' action='' enctype='multipart/form-data'>
<input id='dZUpload' type='file' name='propimage[]' multiple='multiple'/>
<input type='submit' name='uploadimg value='Upload' />
</form>

My PHP code to upload image (only the image upload part):

$folder_name = 'propertyimage/';
$temp_file = $_FILES['propimage']['tmp_name'];
$location = "propertyimage/" . $_FILES['propimage']['name'];
move_uploaded_file($temp_file, $location);

Bascially what I am trying to do here is to store the path of the image not the image itself. But got error asenter image description here

And same data stores into mysql database enter image description here

enter image description here How to replace this /Array with the image file name like propertyimage/someimagename.png

Ayush
  • 9
  • 5
  • Tried your approach did something like: $filename = $_FILES['propimage']['name'][0];$location = "propertyimage/" . $filename; the result only propertyimage/ is now storing not the Array part – Ayush Mar 14 '20 at 15:39
  • 1
    Does this answer your question? [Multiple file upload in php](https://stackoverflow.com/questions/2704314/multiple-file-upload-in-php) – ArSeN Mar 14 '20 at 15:47

1 Answers1

1

You need to to loop through the files array to upload all the files and save names in the Database.

// Count # of uploaded files in array
$total = count($_FILES['propimage']['name']);


$filename_arr = [];
// Loop through each file
for( $i=0 ; $i < $total ; $i++ ) {

  //Get the temp file path
  $tmpFilePath = $_FILES['propimage']['tmp_name'][$i];

  //Make sure we have a file path
  if ($tmpFilePath != ""){
    //Setup our new file path
    $newFilePath = "propertyimage/" . $_FILES['propimage']['name'][$i];

    //Upload the file into the temp dir
    if(move_uploaded_file($tmpFilePath, $newFilePath)) {

      //Handle other code here
      $filename_arr[] = $newFilePath;

    }
  }
}

// To store filenames in DB as comma seprated
$file_names = implode(',', $filename_arr);
var_dump($file_names);
Umer Abbas
  • 1,866
  • 3
  • 13
  • 19
  • Actually I am using dropzoneJS to upload files, the problem is that the images saves into the folder but do not upload its path on the database. The above code also didn't work the propimage field in the database remains empty after form submission. – Ayush Mar 14 '20 at 18:15
  • You must have not used `$file_names` in the insert query for saving comma separated file names in the database ? – Umer Abbas Mar 15 '20 at 04:06
  • Yes I have used and by doing that no data is being inserted into Database. – Ayush Mar 15 '20 at 05:12
  • @Ayush `var_dump($file_names)` right after `implode`. – Umer Abbas Mar 15 '20 at 05:32
  • $file_names = implode(',', var_dump($file_names)); after using this error is showing like Notice: Undefined variable: file_names in functions.php on line 343 NULL Warning: implode(): Invalid arguments passed in functions.php on line 343 – Ayush Mar 15 '20 at 05:45
  • `$file_names = implode(',', $filename_arr);` i just realized i was missing a comma for implode. you don't need to implode `$file_names` again. just `var_dump($file_names);` – Umer Abbas Mar 15 '20 at 07:33
  • string(0) "" now this message is showing and still the propimage row is empty – Ayush Mar 15 '20 at 10:12