in my project i store images(uploaded by user) in a folder, for each respective product.
For example to add a product a form is displayed to user. User fills details about product and selects some images from his computer(5 or more pictures). When submitted i will store product details in a table called Products. For images uploaded, i create a folder like this: http://mysite.com/uploads/123/ here 123 is product id of product that is newly added. And all images are saved inside it.
Questions:
-
how to keep track of images uploaded(or filenames of it) for later displaying? that is, when a new product is added, my script will create a fancy uri text. Eg:
http://mysite.com/this-is-my-new-product. So if user visits this link, it should show all images belonging to that product. For this, I have to keep a separate table with product ids and the filenames ? And query this table and echo filenames from it? Or is it good to scan the folder(example:http://mysite.com/uploads/123/*.jpg) and echo images one by one, ie. without using a separate table ? -
is it ok if use the fancy uri for the filenames of images? Example:
/uploads/123/this-is-my-new-product1.jpg,/uploads/123/this-is-my-new-product2.jpg,/uploads/123/this-is-my-new-product3.jpg, etc. ? So, users will see path of first image ashttp://mysite.com/uploads/123/this-is-my-new-product1.jpg. This is physical address. Will it help in SEO ? Or it is bad practice ? If this is good, then to list all images i should use scanddir() ?
Please guide me through correct path. Thanku
As you say, you either need some link between the data and the images, or be prepared to scan the folder and just output what you find. The latter is more computationally expensive and not altogether graceful as an approach.
A compromise might be to rename the images, as they’re uploaded, 1.jpg, 2.jpg etc and then simply log in the DB the number of images uploaded. Example:
If you were unwilling to rename the images from their uploaded names, you would need to log their names in the DB, presumably in their own table and via a foreign key to the product table.
A final option would be to store the images as blobs in the database, but I’m no DB expert so I don’t know how widespread or advisable this is.