[英]Multiple Upload to MySql Database, foreach not submitting photos
我有一个脚本可以将一个文件上传到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();
}
}
}
}
$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
}
}
}
我认为这可能对您有帮助。 来源: 用php上传多个文件
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.