我正在尝试将多个文件从 HTML 表单上传到一个文件夹并将信息存储在我的数据库中。我已经编写了以下代码,但它无法正常工作。如果我上传 1 张图像并留下其他两个图像字段,则它存储 3 张同名图像和相同图像,如果我在三个不同字段中上传 3 张不同图像,则它只需要最后一张图像并用它替换其他图像,并显示所有人的相同图像。
请指导我。
HTML Image Fields
<file> <Image2> <Image3>
PHP代码
/*-------------------
IMAGE QUERY
---------------*/
$allowedExts = array("gif", "jpeg", "jpg", "png");
$extension = end(explode(".", $_FILES["file"]["name"]));
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["image2"]["type"] == "image/gif")
|| ($_FILES["image3"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["image2"]["type"] == "image/jpeg")
|| ($_FILES["image3"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/jpg")
|| ($_FILES["image2"]["type"] == "image/jpg")
|| ($_FILES["image3"]["type"] == "image/jpg")
|| ($_FILES["file"]["type"] == "image/pjpeg")
|| ($_FILES["image2"]["type"] == "image/pjpeg")
|| ($_FILES["image3"]["type"] == "image/pjpeg")
|| ($_FILES["file"]["type"] == "image/x-png")
|| ($_FILES["image2"]["type"] == "image/x-png")
|| ($_FILES["image3"]["type"] == "image/x-png")
|| ($_FILES["file"]["type"] == "image/png")
|| ($_FILES["image2"]["type"] == "image/png")
|| ($_FILES["image3"]["type"] == "image/png"))
//&& ($_FILES["file"]["size"] < 200000)
&& in_array($extension, $allowedExts))
{
if ($_FILES["file"]["error"] > 0 && $_FILES["image"]["error"] > 0)
{
echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
}
else
{
echo "Upload: " . $_FILES["file"]["name"] . "<br>";
echo "Type: " . $_FILES["file"]["type"] . "<br>";
echo "Size: " . ($_FILES["file"]["size"] / 200000) . " kB<br>";
echo "Upload: " . $_FILES["image2"]["name"] . "<br>";
echo "Type: " . $_FILES["image2"]["type"] . "<br>";
echo "Size: " . ($_FILES["image2"]["size"] / 200000) . " kB<br>";
$image_name= $_FILES["file"]["name"];
$random_name= rand().$_FILES["file"]["name"];
$image_name2= $_FILES["image2"]["name"];
$random_name2= rand().$_FILES["image2"]["name"];
$image_name3= $_FILES["image3"]["name"];
$random_name3= rand().$_FILES["image3"]["name"];
$path= move_uploaded_file($_FILES["file"]["tmp_name"],
"upload/products/" . $random_name);
$path2= move_uploaded_file($_FILES["image2"]["tmp_name"],
"upload/products/" . $random_name);
$path3= move_uploaded_file($_FILES["image3"]["tmp_name"],
"upload/products/" . $random_name);
$folder="upload/products/" .$random_name;
$folder2="upload/products/" .$random_name;
$folder3="upload/products/" .$random_name;
//echo "Stored in: " . "upload/" .rand(). $_FILES["file"]["name"];
echo "Stored in: "."upload/products/". $random_name;
/*
$sql = "INSERT INTO `category_images` (`image_name`,`image_location`) VALUES
('".$image_name."', '".$path."')";
*/
$sql = "Insert into product_images (product_id,name,images)
VALUES ($current_id,'$image_name', '$folder')";
$sql2 = "Insert into product_images (product_id,name,images)
VALUES ($current_id,'$image_name2', '$folder2')";
$sql3 = "Insert into product_images (product_id,name,images)
VALUES ($current_id,'$image_name3', '$folder3')";
$result = mysql_query($sql);
mysql_query($sql2);
mysql_query($sql3);
if ($result)
{
echo "successfull";
}
else {
echo mysql_error();
}
}
}
else
{
echo "Invalid file";
}
/*-----------------
IMAGE QUERY END
------------------*/