Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

storing file information

Status
Not open for further replies.

tyrannus

MIS
Apr 6, 2006
115
US
Hey all, I have a website that I am designing that has a bunch of images in it.. instead of writing a bunch of html code to put these images into a table I wanted to use MYSQL and PHP to do so. I know that storing the images themselves is a bad idea so can anyone give me a hint on how to store the file information (i.e path and filename) into the database.. More like what information i need. also is it possible to have all the filenames in a text file and read the info from there to populate the table??
 
You'll need path and filename stored, possibly best as varchar (and fulltext if you want searchable filenames), also some way of identifying what the images are so that you can call them easily.
Then once done, select what you ned and loop through the resusts wrapping thm in html tags.



______________________________________________________________________
There's no present like the time, they say. - Henry's Cat.
 
Probably a table that looks like:

Code:
ID Filename      Path       Description
1   image1.jpg    \images\      First Image
2  somepict.jpg   \otherpics\   Random Image
...


It is then easy to sort by filename, folder or even description.

The Field types would be varchar.


----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
I'd also add a gallery_id in case you wish to display them as groups, and possibly a catgory - weather, fun, cars, babes etc etc

______________________________________________________________________
There's no present like the time, they say. - Henry's Cat.
 
KarveR, I'd say to do the gallery_id but not in the same table. Create a separate table with image_id and gallery_id only. You can then categorize a photo in more than one gallery by adding a new row for each gallery the photo would be in.

That way when you want all automobiles you can bring up a particular photo, but it doesn't show up when you want to show only classic cars for instance.

 
Thank you all for your suggestions.. I have decided only to have a single simple table. It is only for pictures taken during my last fishing trip so it doesn't have to be complicated..
 
Just as an after thought, is it possible to have MySQL read the file name from the file iteself and store that in the database?? Or, should I get PHP to do that for me
 
MYSQL canot read filenames from files its a DB not a file reading app.

You could however have PHP run through the folder that contains the image files and add the files it finds to the DB "automatically".






----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
Thanks all for your help... Just one more thing.. I have a PHP script that reads the filenames from a directory and places them in a variable.. I then use a loop to process each filename and add it into the database.. only problem is in reading the filenames it also gets the " . " and the " .. ", does mysql allow for only picking the .jpg extensions ??
 
It will be much easier if you limit your PHP code to not include the . and .. when its inserting them into the DB. something like:
Code:
if(($filename!=".") && ($filename!="..")){
[green]Insert code goes here[/green]
}

----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
Similarly you could check the file extension ".jpg", ".gif" etc.
This would also prevent any non picture files getting placed in your DB

Keith
 
If you have php version >= 4.3* glob() is easy

<?php
foreach(glob("*.php") as $file){
$pi=pathinfo("$file");
if($pi[dirname]=='.'){
$path=getcwd();
}else{
$path=$pi[dirname];
}
// submit to DB
echo "Path: $path File: $pi[basename] <br>";

}
?>


______________________________________________________________________
There's no present like the time, they say. - Henry's Cat.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top