当前位置: 动力学知识库 > 问答 > 编程问答 >

mysql - PHP - File path isn't saved to database with file upload

问题描述:

I have a script to handle image uploads and puts the location of the image in the database. There's two image upload fields, named "logo-image" and "header-image". The only time when things go wrong is when I only select a "logo-image". What happens then is that the image gets uploaded, but the file path isn't saved to the database. The error must be somewhere in the code below. Any idea what I am doing wrong?

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");

}

分享给朋友:
您可能感兴趣的文章:
随机阅读: