John
John

Reputation: 31

Store images in file system and use mysql to index

I am sort of a newbie with MySQL. I have a website that will retrieve and allow user to upload photos to the website. I am going to store the photos as a file system inside a folder call \users\images. How would I call the photos from the MySQL using file system? I know people talk about index which make sense but not sure how to code it. If someone has an example or provide me a solution to store photos using file system and display it on the web for each user login to my website.

Upvotes: 2

Views: 2756

Answers (3)

Fathah Rehman P
Fathah Rehman P

Reputation: 8741

Save the image files in \users\images location and save path with filename in mysql table.

The datatype needed would be one that accepts strings, such as VARCHAR for example.

Here is a reference to other types:

Upvotes: 3

mellamokb
mellamokb

Reputation: 56769

You will have to link it all together yourself. Just think through it step-by-step:

  1. User logs in and uploads a photo.
  2. Server (PHP?) receives the photo, with some name, say picture.jpg.
  3. Server looks up the currently logged-in user from Session.
  4. Server checks the database if picture already exists.
  5. Server saves the file to \users\%username%\picture.jpg
  6. Server writes a record linking userid and picture path to a table in the database, say pictures.

Then if you wanted to display the photos to a user.

  1. User logs in and accesses the photo list page (say displayphotos.php).
  2. Server searches pictures table where userid= current user logged in.
  3. Server loops through each record retrieved in the result set.
  4. For each result set, create an <img tag pointing to the path of the picture file
  5. Place the <img tags in some sort of table layout, maybe 4-columns wide.

There isn't some sort of out-of-the-box concept in MySQL that automatically manages filenames that you store in a database pointing to physical files.

Upvotes: 1

Praveen Kumar Purushothaman
Praveen Kumar Purushothaman

Reputation: 167172

Do this way. Upload the images in \users\images\. Have an index in the MySQL Server database with the path too.

+----+---------------------+
| id | path                |
+----+---------------------+
|  1 | \users\images\1.png |
|  2 | \users\images\2.png |
|  3 | \users\images\3.png |
|  4 | \users\images\4.png |
+----+---------------------+

Upvotes: 0

Related Questions