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 biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Store/Display images from MySQL using PHP 1

Status
Not open for further replies.

brogan

Technical User
Oct 7, 2003
44
GB
I have no problem storing and retrieving text data from a MySQL database using PHP. However, I am unsure of how to do this with images. I have thought of storing the relative path and then calling the path displaying the image. What would I store the path as and how would I call it. Basically, I would appreciate it if anybody could give me the absolute basic method of doing this.

Thanks in advance

Mark
 
I have made my own script, as you want to make now.
It's really very simple!

it's basically almost like a guestbook, in it's functionality.

First, you have an upload page. There, you enter title, description, etc. about the image. You might also want the user to choose a category?

Then you use an upload script.
I made my script name the uploaded files a long integer of random chars. I run a loop,
while (is_file($filename . $ext)) {
// generate new random integer;
}

then, after it's uploaded, I simply have another page to view the files.. (For guest viewing).
I store all images in one folder, as I give them random names.

I think it might be a better option to make folders, like:
pictures/username/picturename_randomnumber.ext

I dont know if I would store the path though, as I guess you will anyways dynamically generate the page, on visit.

eg. if you use the picthres/username, you can simply make the script echo out urls like:
echo "<a href=\"?action=view&amp;id={$user_id}\" title=\"\">view images by {$user_id}</a>";

when the user then presses that url, you have some switch or if/else, where you then display images.

I have an included file for image-browsing. The incldued file has three ways to work:
thumb-list, with one thumb (for userlist)
thumb-list, with all thumbs (for looking at one user)
big-image, with one image (when clicked on one picture)

You can there also choose different ways of defining the path to your images.

if you use the username as folder, you always know that:
<img src="pictures/{$username}/{$filename}" /> will work

I however, have made a function that watermarks the images.. eg. I add the URL of my webpage to the bottom, right corner of all images. This is done on-the-fly, eg. I do not replace the originals uploaded by the users.

I also give my users ability to "Optimize" images on the upload page, so they can optimize how many pictures they can have on theire account. (I give different userlevels, which gives both titles and space on the hd).

I hope this gave you some ideas.

Olav Alexander Mjelde
Admin & Webmaster
 
Since the path to the image is usually fixed, you can store the image name in the db. Then simply echo out the result

Code:
...
while ($rows = mysql_fetch_array($result)){
//assign the image name to a var
   $img = $rows['image_name'];
   ...
//echo the image
   echo "<td><img src=\"path/to/image/$img\">";

   ...

}//end while

Bastien

Cat, the other other white meat
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top