Paul Ruocco Paul Ruocco - 2 months ago 19
MySQL Question

Multiple Upload to MySql Database, foreach not submitting photos

I had a script to upload a single file to a MySQL database that I tried to alter to upload multiple files, but I can't seem to get the foreach statement to check and insert each file.

HTML Form

<form id="add_photo_form" class="form-inline" action="<?php $_SERVER['PHP_SELF']; ?>" method="post" enctype="multipart/form-data">

<input type="hidden" name="album_id" value="<?php echo $_GET['album_id']; ?>" />

<div class="form-group">
<label for="photo_file">Select Photo <span class="required">*</span></label>
<input class="form-control" type="file" name="photo_file[]" multiple="multiple" />
</div>

<div class="form-group text-right">
<input class="btn btn-primary" type="submit" name="add_photo_submit" value="Submit" />
</div>

</form>


PHP Script

if (isset($_POST['add_photo_submit']))
{
foreach($_FILES['photo_file'] as $new_photo)
{
$file_ext = pathinfo('./albums/img/photos/'.basename($new_photo['name']),PATHINFO_EXTENSION);
$file_name = 'image_'.date('mdyHis').uniqid().'.'.$file_ext;
$file_path = './albums/img/photos/'.$file_name;
$album_id = $_POST['album_id'];
$photo_file = $file_name;

if (!empty($new_photo['name']))
{
$uploadOk = 1;
$check = getimagesize($new_photo['tmp_name']);
if ($check == false)
{
$uploadError = 'This is not a valid image.';
$uploadOk = 0;
}
if (file_exists($file_path))
{
$uploadError = 'This file already exists.';
$uploadOk = 0;
}
if ($new_photo['size'] > 1000000000000000000) /* bytes */
{
$uploadError = 'The file is too large.';
$uploadOk = 0;
}
if ($file_ext != 'jpg' && $file_ext != 'jpeg' && $file_ext != 'png' && $file_ext != 'gif')
{
$uploadError = 'Only JPG, JPEG, PNG, or GIF images are allowed.';
$uploadOk = 0;
}
if ($insert = $db -> prepare("INSERT INTO photos (album_id, photo_file) VALUES (?, ?)"))
{
$insert -> bind_param('ss', $album_id, $photo_file);
if ($uploadOk == 1)
{
move_uploaded_file($new_photo['tmp_name'], $file_path);
if ($insert -> execute() == true)
{
echo '<div class="alert alert-success" role="alert"><span class="icon icon-arrows-check"></span> A new album was created.</div>';
}
else
{
echo '<div class="alert alert-danger" role="alert"><span class="icon icon-arrows-deny"></span> '.$insert -> error.'</div>';
}
}
else
{
echo '<div class="alert alert-danger" role="alert"><span class="icon icon-arrows-deny"></span> '.$uploadError.'.</div>';
}
$insert -> close();
}
}

}
}


If I remove the
[]
from
photo_file[]
in my HTML form and remove the foreach statement in the PHP script, I am able to upload a single image with no problems.

When I try to upload, I get no database error, no statement error, no file error, and no error log.

How can I fix my foreach statement to check and upload each image?


EDIT
As per the accepted answer below, my new php script looks like the following. Hopefully this can help someone down the road.


if (isset($_POST['add_photo_submit']))
{
$total = count($_FILES['photo_file']['name']);
for($i=0; $i<$total; $i++)
{
$file_ext = pathinfo('./albums/img/photos/'.basename($_FILES['photo_file']['name'][$i]),PATHINFO_EXTENSION);
$file_name = 'image_'.date('mdyHis').uniqid().'.'.$file_ext;
$file_path = './albums/img/photos/'.$file_name;
$album_id = $_POST['album_id'];
$photo_file = $file_name;

if (!empty($_FILES['photo_file']['name'][$i]))
{
$uploadOk = 1;
$check = getimagesize($_FILES['photo_file']['tmp_name'][$i]);
if ($check == false)
{
$uploadError = 'This is not a valid image.';
$uploadOk = 0;
}
if (file_exists($file_path))
{
$uploadError = 'This file already exists.';
$uploadOk = 0;
}
if ($_FILES['photo_file']['size'][$i] > 1000000000000000000) /* bytes */
{
$uploadError = 'The file is too large.';
$uploadOk = 0;
}
if ($file_ext != 'jpg' && $file_ext != 'jpeg' && $file_ext != 'png' && $file_ext != 'gif')
{
$uploadError = 'Only JPG, JPEG, PNG, or GIF images are allowed.';
$uploadOk = 0;
}
if ($insert = $db -> prepare("INSERT INTO photos (album_id, photo_file) VALUES (?, ?)"))
{
$insert -> bind_param('ss', $album_id, $photo_file);
if ($uploadOk == 1)
{
move_uploaded_file($new_photo['tmp_name'], $file_path);
if ($insert -> execute() == true)
{
echo '<div class="alert alert-success" role="alert"><span class="icon icon-arrows-check"></span> A new album was created.</div>';
}
else
{
echo '<div class="alert alert-danger" role="alert"><span class="icon icon-arrows-deny"></span> '.$insert -> error.'</div>';
}
}
else
{
echo '<div class="alert alert-danger" role="alert"><span class="icon icon-arrows-deny"></span> '.$uploadError.'.</div>';
}
$insert -> close();
}
}
}
}

Answer
$total = count($_FILES['photo_file']['name']);

// Loop through each file
for($i=0; $i<$total; $i++) {
  //Get the temp file path
  $tmpFilePath = $_FILES['photo_file']['tmp_name'][$i];

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

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

      //Handle other code here

    }
  }
}

i think this may help you. Origin: Multiple file upload in php