...

View Full Version : Store Image in Database



Cerby
10-28-2007, 11:55 AM
I have the following code that uploads images to the server. How do I store the images in the database? What options do I have?


<?php
if (($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/bmp")
|| ($_FILES["file"]["type"] == "image/pjpeg")
&& ($_FILES["file"]["size"] < 20000))
{
if ($_FILES["file"]["error"] > 0)
{
echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
}
else
{
echo "Upload: " . $_FILES["file"]["name"] . "<br />";
echo "Type: " . $_FILES["file"]["type"] . "<br />";
echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />";

if (file_exists("upload/" . $_FILES["file"]["name"]))
{
echo $_FILES["file"]["name"] . " already exists. ";
}
else
{
move_uploaded_file($_FILES["file"]["tmp_name"],
"upload/" . $_FILES["file"]["name"]);
echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
}
}
}
else
{
echo "Invalid file";
}
?>

iceflyin
10-28-2007, 12:23 PM
Cerby, people would prefer if you used the mighty G to answer questions like these. :) Simple beginner stuff is available anywhere.

http://www.google.com/search?q=php+store+image+in+database

Cerby
10-28-2007, 12:31 PM
I not asking how to do it, I just want to know what options are available?

abduraooft
10-28-2007, 12:58 PM
One way is to store the path(dir+file_name) of the uploaded file in a DB table.

firepages
10-29-2007, 01:23 AM
2 options, only 1 of them good ;)
bad one is to store the data in a binary field in your DB
good one is as already mentioned to copy to the filesystem leaving either a path in the DB or use some logical naming convention that relates to the DB id field etc.

If you have googled at all you will already have read the reasons why storing the image data itself in a DB is a daft idea.

iceflyin
10-29-2007, 01:32 AM
I just create a folder for each user and upload it there, and store the path in the database...

Eg: Files goto:
userdata/user1/images/imagename.jpg
userdata/user2/images/imagename.gif

And, what gets stored in the database is "user2/images/imagename.jpg"

If you store the username in a separate field on the same row as the image name... You don't need to store the path. All you will be storing is in the rows of the images table is: "id"(auto incrementing primary key), "username", and "imagename.extension"



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum