I've followed an online tutorial for storing an image into a MySQL database.
I have created a table (databaseimage) with three fields id (primary key, auto increment), name, image (mediumblob type).
The php is supposed to display the image after it has been uploaded but I get the problem uploading image from the if then else statement. Also the full image name including the name of the tmp dir is displayed but the php is only supposed to display the filename. Despite these issues I can see the images inside the database with the correct filenames and sizes!
Is there a reason why you want to store the images in the database (BLOB) and
not store the images in a directory and use the table for storing the image filenames?
In other words, do you really need to store the images/photos in your database table?
Is there a reason why you want to store the images in the database (BLOB) and
not store the images in a directory and use the table for storing the image filenames?
In other words, do you really need to store the images/photos in your database table?
.
I'd like to know how to do it before exploring storing the images into a dir which looks more complicated and the images will relate to an individual's record in the database.
Is there a reason why you want to store the images in the database (BLOB) and
not store the images in a directory and use the table for storing the image filenames?
In other words, do you really need to store the images/photos in your database table?
Believe it or not storing in a db can actually be a better idea. Say you delete a user from the DB and then their images from the disk. If you do a rollback, the users account can be restored but those images that were wiped from the system are gone for good!
Of course then you have the problem of a swelling mysql table
//Please don't use this for your form processing:
if (isset($_POST['submit']))
//Internet explorer has a bug and does not always send the submit value.
Believe it or not storing in a db can actually be a better idea. Say you delete a user from the DB and then their images from the disk. If you do a rollback, the users account can be restored but those images that were wiped from the system are gone for good!
Of course then you have the problem of a swelling mysql table
My problem is I can't get this code to work correctly!
To save an image to a database you should use SQL to read the file into the field like this:
Code:
INSERT INTO databaseimage VALUES ('','$image_name',LOAD_FILE($file))
That way you do away with the PHP code to try to convert it from a file into data that can be inserted into the database. You can also use the SQL DUMPFILE command to write the field from the database directly into a file again. The file in the database should have all of the headers etc included with it (as happens when you use LOAD_FILE) as otherwise the image can be easily broken.
I assume the autoincrement field is there to allow for multiple images with the same name as otherwise the image name could be used as the primary key.
//Please don't use this for your form processing:
if (isset($_POST['submit']))
//Internet explorer has a bug and does not always send the submit value.
To save an image to a database you should use SQL to read the file into the field like this:
Code:
INSERT INTO databaseimage VALUES ('','$image_name',LOAD_FILE($file))
That way you do away with the PHP code to try to convert it from a file into data that can be inserted into the database. You can also use the SQL DUMPFILE command to write the field from the database directly into a file again. The file in the database should have all of the headers etc included with it (as happens when you use LOAD_FILE) as otherwise the image can be easily broken.
I assume the autoincrement field is there to allow for multiple images with the same name as otherwise the image name could be used as the primary key.
Parse error: syntax error, unexpected T_STRING in imageupload.php on line 36
Code:
if ($image_size==FALSE)
echo "That's not an image.";
else
{
if ($insert = mysql_query("INSERT INTO databaseimage VALUES ('','$image_name',LOAD_FILE($file)))
echo "Problem uploading image";
else
Parse error: syntax error, unexpected T_STRING in imageupload.php on line 36
Code:
if ($image_size==FALSE)
echo "That's not an image.";
else
{
if ($insert = mysql_query("INSERT INTO databaseimage VALUES ('','$image_name',LOAD_FILE($file)))
echo "Problem uploading image";
else
Don't I need to declare the file variable?
That you do and you need to fix the syntax of the snippet you posted. Instead of using code tags use PHP tags and it'll become pretty obvious what is wrong a good portion of the time.
PHP Code:
if ($image_size==FALSE) {
echo "That's not an image.";
} elseif ($insert = mysql_query("INSERT INTO databaseimage VALUES ('','$image_name',LOAD_FILE($file)")) {
echo "Problem uploading image";
} else {
// do something else here if you need to
}
__________________
Dave .... HostMonster for all of your hosting needs