0

我的 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/ 文件夹。但是它在photossql 表中插入了 4 条记录(这是正确的,因为我选择了 4 张图像)并且所有这些记录也是正确的(我的意思是所有这些记录对于每个正确的图像都有单独的数据)。如果我选择 2 张图片,那么它只会上传一张到 ./uploads/photos/ 文件夹,但是它会在 sql 表中插入 2 条记录。

为什么会显示这个问题?请帮忙!

4

2 回答 2

0

将您的 PHP 代码更改为此;

if(isset($_FILES['image']))
{
    $count = 0;
    foreach($_FILES['image']['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.");
            continue;
        }

        if($file_size > 2097152)
        {
            $size_in_kb = $file_size/1000;
            error("File size must be less than 2MB.");
            continue;
        }

        if(move_uploaded_file($file_tmp,"uploads/photos/".$tmp_name))
        {
            $insert_array = array("uid" => $uid,    "image" => $tmp_name,   "approved" => '1',  "likes" => '',  "dateline" => TIME_NOW);
            $db->insert_query("photos", $insert_array);
            $count++;
        }
    }
}
redirect("post_photo.php?action=post_photo", "Photos have been posted successfully.");

它应该工作!

于 2013-10-23T13:58:36.740 回答
0

您说数据库中添加了 4 条记录,只上传了 2 张图片。这是问题所在:

    if(is_dir("uploads/photos/".$file_name)==false)
    {
        //1
        move_uploaded_file($file_tmp,"uploads/photos/".$file_name);
    }
    //2
    $db->insert_query("photos", $insert_array);

如您所见,步骤 //2 始终执行,但步骤 //1 仅在此条件为真时执行:"is_dir("uploads/photos/".$file_name)==false"。

解决方法:在执行sql语句之前检查if(move_uploaded_file(......))。

于 2013-10-23T08:22:58.867 回答