Need help with uploading images and MySQL
Posted: Wed Nov 14, 2007 7:09 pm
What I want to do is have a form that uploads an image to a file (that works perfectly) and submits the URL and the Name into the database (that doesn't work) and than the script would create an individual page for the image (that works).
I chose to take the MySQL route on this one... why does the following NOT submit the $imageUrl into the database?
By the way... the values for $dbhost, $dbuser, $dbpass, and $dbtable are in a config file so you know up front 
I chose to take the MySQL route on this one... why does the following NOT submit the $imageUrl into the database?
Code: Select all
<?php
//setting the values
$name = $_POST['file-name'];
//The uploading code
if (($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/pjpeg")
|| ($_FILES["file"]["type"] == "image/bmp")
|| ($_FILES["file"]["type"] == "image/png")
&& ($_FILES["file"]["size"] < 20000))
{
if ($_FILES["file"]["error"] > 0)
{
echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
}
else
{
echo "<p>Upload: " . $_FILES["file"]["name"] . "<br />";
echo "Type: " . $_FILES["file"]["type"] . "<br />";
echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb</p>";
if (file_exists("images/" . $_FILES["file"]["name"]))
{
echo $_FILES["file"]["name"] . " already exists. ";
$error = true;
}
else
{
move_uploaded_file($_FILES["file"]["tmp_name"],
"images/" . $_FILES["file"]["name"]);
echo 'Stored in: ' . 'images/' . $_FILES["file"]["name"];
$imageUrl = 'images/' . $_FILES["file"]["name"];
$error = false;
}
}
}
else
{
echo "<p>Invalid file</p>";
$error = true;
}
//Creating the file
if($error === false)
{
//MySQL connection
mysql_connect($dbhost, $dbuser, $dbpass) or die(mysql_error());
//Database connection
mysql_select_db($dbtable) or die(mysql_error());
mysql_query('INSERT INTO imageSharing (`url`,`url`) VALUES ('$imageUrl','$name')');
//Creating the image file
$filename2 = 'pages/'. $_FILES["file"]["name"] .'.php';
$fp2 = fopen('"$filename2", "w");
fwrite($fp2, "
<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">
<html xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"en\" lang=\"en\">
<head>
<title>". $name ."</title>
<meta name=\"keywords\" content=\"". $keywords .">\" />
<meta name=\"description\" content=\"". $name ."\" />
<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />
<link rel=\"stylesheet\" href=\"../". $cssScript ."\" title=\"". $siteName .">\" media=\"screen\" type=\"text/css\" />
</head>
<body>
<div id=\"wrap\">
<div id=\"header\">". $header ."</div>
<div id=\"container\">
<div id=\"navigation\">
<a href=\"../Index.php\">Home</a>
<a href=\"../Submit.php\">Submit</a>
<a href=\"../Images.php\">Images</a>
<a href=\"../Search.php\">Search</a>
</div>
<div id=\"content\">
<img src=\"../". $imageUrl ."\" alt=\"". $name ."\" /><br /><p class=\"alt\">". $name ."</p>
</div>
</div>
<div id=\"footer\">". $footer ."</div>
</div>
</body>
</html>");
chmod("$filename2", 0755);
fclose($fp2);
//MySQL log off
mysql_close();
}
else
{
print "There was an error.";
}
?>