我的 HTML 代码是;
<form action="post_photo.php" method="post" enctype="multipart/form-data">
<tr>
<td class="trow1" width="10%">Select Image:</td>
<td class="trow1" width="90%"><input type="file" name="image[]" multiple="multiple" tabindex="1"></td>
</tr>
<tr>
<td class="trow2" align="left">
<input type="hidden" name="action" value="do_post_photo" />
<input type="hidden" name="uid" value="{$uid}" />
<input type="submit" class="button" name="submit" value="Upload Photos" tabindex="2">
</td>
</tr>
</form>
我的 post_photo.php 文件有这个代码;
if(isset($_FILES['image']))
{
foreach($_FILES['image']['tmp_name'] as $key => $tmp_name )
{
$file_size = $_FILES['image']['size'][$key];
$file_tmp = $_FILES['image']['tmp_name'][$key];
$ext = get_extension($_FILES['image']['name'][$key]);
$allowed_ext = explode(",","png,gif,jpg,jpeg");
if(!in_array($ext,$allowed_ext))
{
error("Invalid extension.");
}
if($file_size > 2097152)
{
error("File size must be less than 2 MB.");
}
$file_name = "photo_".TIME_NOW.".".$ext;
$insert_array = array(
"uid" => $uid,
"image" => $file_name,
"approved" => '1',
"likes" => '',
"dateline" => time()
);
if(is_dir("uploads/photos/".$file_name)==false)
{
move_uploaded_file($file_tmp,"uploads/photos/".$file_name);
}
$db->insert_query("photos", $insert_array);
}
}
redirect("post_photo.php?action=post_photo", "Photos have been posted successfully.");
问题是,如果我选择 4 张图片,它会将 2 张图片上传到 ./uploads/photos/ 文件夹。但是它在photos
sql 表中插入了 4 条记录(这是正确的,因为我选择了 4 张图像)并且所有这些记录也是正确的(我的意思是所有这些记录对于每个正确的图像都有单独的数据)。如果我选择 2 张图片,那么它只会上传一张到 ./uploads/photos/ 文件夹,但是它会在 sql 表中插入 2 条记录。
为什么会显示这个问题?请帮忙!