我有一个脚本将单个文件上传到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();
}
}
}
}
如果我从HTML表单中的[]
中删除photo_file[]
并删除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 :(得分:1)
Msg 102, Level 15, State 1, Line 2
Incorrect syntax near '('.
我认为这可能会对你有所帮助。 来源:Multiple file upload in php