2016-09-15 80 views
0

我有一个脚本上传单个文件到MySQL数据库,我试图改变上传多个文件,但我似乎无法得到foreach语句来检查和插入每个文件。多次上传到MySql数据库,foreach不提交照片

HTML表单

<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脚本

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(); 
      } 
     } 

    } 
} 

如果我删除从photo_file[][]在我的HTML表单,并删除在PHP脚本foreach语句,我能上传单一图像没有问题。

当我尝试上传时,我没有收到数据库错误,没有语句错误,没有文件错误,也没有错误日志。

如何解决我的foreach语句以检查并上传每个图像?

编辑 按照接受的答案下面,我的新的PHP脚本如下所示。希望这可以帮助某个人。

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(); 
      } 
     } 
    } 
} 
+0

首先查看$ _FILES数据结构实际包含的内容(var_dump) – CBroe

回答

1
$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 

    } 
    } 
} 

我认为这可以帮助你。 原点:Multiple file upload in php