Page 1 of 1
Which Image databasing solution
Posted: Sun Nov 20, 2005 6:11 am
by mhouldridge
Hi,
I am currently developing a real estate sytstem which will allow agents / users to upload images of their homes.
I should expect that there will be quite a number of postings per week and I am not sure which approach to take.
I understand that you can store image data within a table, allowing for an auto-increment field, therefore elimating chances of the same image name being uploaded.
I am also aware that you can upload to a directory and use the file path within a database table to call the image. I think the first option would be better, however I am concerned that the first solution is geared towards smaller images, like avatars., some images will be quite large you see....
Please help!
Posted: Sun Nov 20, 2005 7:22 am
by m3mn0n
My personal preference is folders with .htaccess files for added security & hot link protection activated.
And I believe the database solution is also good for normal sized images.
Posted: Sun Nov 20, 2005 7:23 am
by yum-jelly
The second option is always better. The server has better ways of serving images than you can ever think in scripting! Just hold the paths to the images in the database. The only time I would ever recommend image storage in a database is when you have no way of protecting a image by placing them off site, such as when you might be on shared hosting and the host only allows you to keep things in your document root.
yj
Posted: Sun Nov 20, 2005 1:29 pm
by AKA Panama Jack
The huge drawback to storing images inside a database is speed. Especially when the images start getting fairly large. When it comes to images you should store them in their own directory.
There are many ways to prevent duplicate image names. You can create a master image directory and then create subdirectories for each agent using an ID number (Agent_1). The agents will probably be required to log into your system so they can add/edit/delte their listings so that means they will be stored in an agent table. Use an auto increment field for the agent ID number so each agent gets their own ID. This way each agent has their own image directory and it will be easier for you to delete all images for an agent that leaves the company.
If you are worried about image names you can rename the file as you move it from the temp directory it was uploaded to the agents directory and store the new name in the database for that listing. One simple way it to add a unix timestamp (time) or date (date) onto the end of the filename. So house.gif becomes house10-10-2005.gif or even agent_1_10-10-2005.gif if you decide not to use subdirectories for agent images.
Posted: Sun Nov 20, 2005 1:30 pm
by mhouldridge
Excellent, thanks for that!
I have managed to create an upload form which works, based on a tutorial I found on a google search.
The code looks like this;
Code: Select all
<?
if ($userfile_size >250000){$msg=$msg."Your uploaded file size is more than 250KB so please reduce the file size and then upload. Visit the help page to know how to reduce the file size.<BR>";
$file_upload="false";}
if (!($userfile_type =="image/pjpeg" OR $userfile_type=="image/gif")){$msg=$msg."Your uploaded file must be of JPG or GIF. Other file types are not allowed<BR>";
$file_upload="false";}
$add="uploads/$userfile_name"; // the path with the file name where the file will be stored, upload is the directory name.
if(move_uploaded_file ($userfile, $add)){
echo "File uploaded, thankyou";
}
else{echo "Failed to upload file Contact Site admin to fix the problem";
}
?>
Ok, so I havent stored anything in the database yet, I can do that bit shortly, no probs (fingers crossed).
I have one more question that maybe you could help me with. I need to check whether the filename exists first, within my directory. Do you know how I could implement this into my script.
Again, many thanks!
Posted: Mon Nov 21, 2005 6:34 am
by twigletmac
mhouldridge wrote:I have one more question that maybe you could help me with. I need to check whether the filename exists first, within my directory. Do you know how I could implement this into my script.
Have a shifty around PHP's
filesystem functions - things like
file_exists() will probably come in handy.
Mac