PHP - 文件路径未保存到具有文件上载的数据库

时间:2013-09-25 19:00:32

标签: php mysql file-upload

我有一个处理图像上传的脚本,并将图像的位置放在数据库中。有两个图像上传字段,名为“logo-image”和“header-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"); 
 }

0 个答案:

没有答案