我有一个脚本来处理图像上传并将图像的位置放入数据库中。有两个图片上传字段,名为“logo-image”和“header-image”。唯一出现问题的时候是我只选择了一个“logo-image”。然后发生的是图像被上传,但文件路径没有保存到数据库中。错误必须在下面的代码中。知道我做错了什么吗?
if(file_exists($root."/upload/".$_FILES["header-image"]["name"]))
{
$filename = explode(".",$_FILES['header-image']['name']);
$randomnumber = rand(0, 10000);
$imageName = $filename[0].$randomnumber.".".$filename[1];
}
else
{
$imageName = $_FILES['header-image']['name'];
}
$image = mysql_real_escape_string(htmlspecialchars("/upload/".$imageName));
if(file_exists($root."/upload/".$_FILES["logo-image"]["name"]))
{
$filename2 = explode(".",$_FILES['logo-image']['name']);
$randomnumber2 = rand(0, 10000);
$imageName2 = $filename2[0].$randomnumber2.".".$filename2[1];
}
else
{
$imageName2 = $_FILES['logo-image']['name'];
}
$image2 = mysql_real_escape_string(htmlspecialchars("/upload/".$imageName2));
if (move_uploaded_file($_FILES["logo-image"]["tmp_name"], "./upload/".$imageName2) && move_uploaded_file($_FILES["header-image"]["tmp_name"], "./upload/".$imageName)) {
// save the data to the database
mysql_query("INSERT verenigingen SET title='$titlename', description='$contentname', url='$urlname', omslag='$image', logo='$image2'")
or die(mysql_error());
// once saved, redirect back to the view page
header("Location: verenigingenindex.php");
}
elseif (move_uploaded_file($_FILES["logo-image"]["tmp_name"], "./upload/".$imageName2) || move_uploaded_file($_FILES["header-image"]["tmp_name"], "./upload/".$imageName)) {
// save the data to the database
mysql_query("INSERT verenigingen SET title='$titlename', description='$contentname', url='$urlname', omslag='$image', logo='$image2'")
or die(mysql_error());
// once saved, redirect back to the view page
header("Location: verenigingenindex.php");
}
else {
// save the data to the database
mysql_query("INSERT verenigingen SET title='$titlename', description='$contentname', url='$urlname'")
or die(mysql_error());
// once saved, redirect back to the view page
header("Location: verenigingenindex.php");
}