Hello,
I am a newbie and I have to get an image uploading and saving to a mysql database. I have looked at more information and still cannot figure out what I am doing wrong. I am running windows IIS and php 5.2. Could someone take a look at my code to see what I have not done correctly please. It's basic I am just trying to get a record to save in the database, and once that is accomplished I will put in all the neccessary safe guards.
Thanks very much !! I am grateful!!
<html>
<head>
<basefont face="Arial">
<title>File Upload To Database</title>
</head>
<body>
<h3>Please Choose a File and click Submit</h3>
<?php
if (!isset($_POST['submit'])) {
// form not submitted
?>
<form enctype="multipart/form-data" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<input type="hidden" name="MAX_FILE_SIZE" value="10000000" />
<input name="userfile[]" type="file" />
<input type="submit" value="Submit" />
</form>
<?php
}
else {
// form submitted
// set server access variables
$host = "localhost";
$user = "root";
$pass = "XXX";
$db = "YYY";
// get form input
// check to make sure it's all there
// escape input values for greater safety
$imagetype = $_FILES["userfile"]["type"];
$imagename = $_FILES["userfile"]["name"]; ;
$image = addslashes(file_get_contents($_FILES["userfile"]["tmp_name"]));
$imagesize = $_FILES["userfile"]["size"];
$imagectgy = $_FILES["userfile"]["type"];
// open connection
$connection = mysql_connect($host, $user, $pass) or die ("Unable to connect!");
// select database
mysql_select_db($db) or die ("Unable to select database!");
// create query
$query = "INSERT INTO images (image_type, image,image_size,image_ctgy,image_name) VALUES ('$imagetype', '$image','$imagesize','$imagectgy','$imagename')";
// execute query
$result = mysql_query($query) or die ("Error in query: $query. ".mysql_error());
// print message with ID of inserted record
echo "New record inserted with ID ".mysql_insert_id();
// close connection
//mysql_close($connection);
}
?>
</body>
</html>
save image as blob in mysql
Moderator: General Moderators