我有一个简单的代码来上传多个图像,将图像上传到文件夹并保存数据库的路径。我遇到的问题是图像名称将保存到数据库,但图像不会上传到该文件夹。这是我正在使用的代码。它是一个免费的手动代码(没有太多的HTML),所以你几乎可以尝试它。我的数据库由id,image1,image2,image3组成。
以下是上传代码
<?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'];
$temp=$_FILES['photo']['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($temp,"images/".$img1);
move_uploaded_file($temp,"images/".$img2);
move_uploaded_file($temp,"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>
一切似乎都很好,但图像仍然没有上传到指定的文件夹。请帮帮我们。
*****解决****** 解决方案:我只需将其放在我的代码中。
$temp1=$_FILES['photo1']['tmp_name'];
$temp2=$_FILES['photo2']['tmp_name'];
$temp3=$_FILES['photo3']['tmp_name'];
这就是我做的......
<?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>
现在,图像很好地上传到文件夹,并将名称保存到数据库中。 感谢斯拉维奇指出它。
答案 0 :(得分:0)
您似乎正在尝试移动未上传的文件。
<input type="file" name="photo1" id="photo"><br />
<input type="file" name="photo2" id="photo"><br />
<input type="file" name="photo3" id="photo"><br />
然后在你的PHP中你有:
$temp=$_FILES['photo']['tmp_name'];
// this would have to be something you have uploaded in your form:
// $_FILES['photo1']['tmp_name'];
通常,Power Programming在这些情况下有帮助。使用好友(他在编程时不一定比你更好,但如果是的话,它确实有帮助),并尽可能向他解释问题。接下来你知道了 - 你已经解决了你的问题。
P.S。与您的显式问题无关,在html中不应该有重复的id值。你有三个重复的ID。
答案 1 :(得分:0)
我认为它是这样的:
<body>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>" enctype="multipart/form-data">
<p> Upload Image<br />
<input type="file" name="photo[]" id="photo"><br />
<input type="file" name="photo[]" id="photo"><br />
<input type="file" name="photo[]" id="photo"><br />
<input type="submit" name="submit" id="submit" value="Add Product" style="margin-top: 25px; margin-left: 335px;"/>
</p>
</body>
</html>
迭代数组$ _FILES [&#39; photo&#39;] !!
答案 2 :(得分:0)
您必须将名称定义为 name =&#34; photo []&#34;
应该是:
<input type="file" name="photo[]" id="photo"><br />
<input type="file" name="photo[]" id="photo"><br />
<input type="file" name="photo[]" id="photo"><br />