我有一个简单的代码来上传多个图像,将图像上传到文件夹并保存数据库的路径。问题是我有3个浏览按钮。当使用图像选择所有三个浏览按钮时,脚本仅上载文件并将路径保存到数据库。但是,当我只选择1个图像上传脚本时不起作用。怎么了?
这是我目前的剧本。
<?php
include'includes/db.php';
if(isset($_POST['submit'])){
$extension = substr($_FILES['photo1']['name'],
strrpos($_FILES['photo1']['name'], '.'));
$extension = substr($_FILES['photo2']['name'],
strrpos($_FILES['photo2']['name'], '.'));
$extension = substr($_FILES['photo3']['name'],
strrpos($_FILES['photo3']['name'], '.'));
$extension = strtolower($extension);
echo $extension;
if( $extension == ".jpg" || $extension == ".jpeg" || $extension == ".gif" || $extension == ".png" )
{
$img1=$_FILES['photo1']['name'];
$img2=$_FILES['photo2']['name'];
$img3=$_FILES['photo3']['name'];
$size=$_FILES['photo']['size'];
$type=$_FILES['photo']['type'];
$temp1=$_FILES['photo1']['tmp_name'];
$temp2=$_FILES['photo2']['tmp_name'];
$temp3=$_FILES['photo3']['tmp_name'];
$limit_size = 1024000;
$size_in_kb = 1024;
$max_size = $limit_size/$size_in_kb;
if($size > $limit_size)
{
echo "<script>location.replace('test.php?err=File size exceeds $max_size KB')</script>";
}
else
{
move_uploaded_file($temp1,"images/".$img1);
move_uploaded_file($temp2,"images/".$img2);
move_uploaded_file($temp3,"images/".$img3);
$sql2="INSERT INTO ad_images(image1, image2, image3)VALUES('$img1', '$img2', '$img3')";
$res2=mysql_query($sql2);
if($res2){
echo "<script>location.replace('test.php?success=Product added successfuly')</script>";
}else{
echo "<script>location.replace('test.php?vlx=Error. Try Again...')</script>";
}
}
}
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Script Testing</title>
</head>
<body>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>" enctype="multipart/form-data">
<p> Upload Image<br />
<input type="file" name="photo1" id="photo"><br />
<input type="file" name="photo2" id="photo"><br />
<input type="file" name="photo3" id="photo"><br />
<input type="submit" name="submit" id="submit" value="Add Product" style="margin-top: 25px; margin-left: 335px;"/>
</p>
</body>
</html>
我的代码中没有出错,一切似乎都没问题。请帮忙。
************已解决***************
我使用相同的变量扩展名。现在解决了。
这是我的新代码。
<?php
include'includes/db.php';
if(isset($_POST['submit'])){
$extension1 = substr($_FILES['photo1']['name'],
strrpos($_FILES['photo1']['name'], '.'));
$extension2 = substr($_FILES['photo2']['name'],
strrpos($_FILES['photo2']['name'], '.'));
$extension3 = substr($_FILES['photo3']['name'],
strrpos($_FILES['photo3']['name'], '.'));
$extension1 = strtolower($extension1);
echo $extension1;
$extension2 = strtolower($extension2);
echo $extension2;
$extension3 = strtolower($extension3);
echo $extension3;
if( $extension1 == ".jpg" || $extension1 == ".jpeg" || $extension1 == ".gif" || $extension1 == ".png" ||
$extension2 == ".jpg" || $extension2 == ".jpeg" || $extension2 == ".gif" || $extension2 == ".png" ||
$extension3 == ".jpg" || $extension3 == ".jpeg" || $extension3 == ".gif" || $extension3 == ".png" )
{
$img1=$_FILES['photo1']['name'];
$img2=$_FILES['photo2']['name'];
$img3=$_FILES['photo3']['name'];
$size=$_FILES['photo']['size'];
$type=$_FILES['photo']['type'];
$temp1=$_FILES['photo1']['tmp_name'];
$temp2=$_FILES['photo2']['tmp_name'];
$temp3=$_FILES['photo3']['tmp_name'];
$limit_size = 1024000;
$size_in_kb = 1024;
$max_size = $limit_size/$size_in_kb;
if($size > $limit_size)
{
echo "<script>location.replace('test.php?err=File size exceeds $max_size KB')</script>";
}
else
{
move_uploaded_file($temp1,"images/".$img1);
move_uploaded_file($temp2,"images/".$img2);
move_uploaded_file($temp3,"images/".$img3);
$sql2="INSERT INTO ad_images(image1, image2, image3)VALUES('$img1', '$img2', '$img3')";
$res2=mysql_query($sql2);
if($res2){
echo "<script>location.replace('test.php?success=Product added successfuly')</script>";
}else{
echo "<script>location.replace('test.php?vlx=Error. Try Again...')</script>";
}
}
}
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Script Testing</title>
</head>
<body>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>" enctype="multipart/form-data">
<p> Upload Image<br />
<input type="file" name="photo1" id="photo"><br />
<input type="file" name="photo2" id="photo"><br />
<input type="file" name="photo3" id="photo"><br />
<input type="submit" name="submit" id="submit" value="Add Product" style="margin-top: 25px; margin-left: 335px;"/>
</p>
</body>
</html>
答案 0 :(得分:0)
您可以使用多个属性上传多个文件,抛出一个浏览按钮,例如
<input type="file" name="img" multiple>
您可以使用ctrl选择多个图像
答案 1 :(得分:0)
因为你使用相同的变量$ extension,如果不选择第三个浏览字段,提交后它将为空白,如果条件不会进入上传。如果您只从第三个浏览按钮浏览,那么将上传该单个文件。