0

What are some ideas out there for storing images on web servers. Im Interacting with PHP and MySQL for the application.

Question 1

Do we change the name of the physical file to a000000001.jpg and store it in a base directory or keep the user's unmanaged file name, i.e 'Justin Beiber Found dead.jpg'? For example

wwroot/imgdir/a0000001.jpg

and all meta data in a database, such as FileName and ReadableName, Size, Location, etc. I need to make a custom Filemanager and just weighing out some pros and cons of the underlying stucture of how to store the images.

Question 2

How would I secure an Image from being downloaded if my app/database has not set it to be published/public?

In my app I can publish images, or secure them from download, if I stored the image in a db table I could store it as a BLOB and using php prevent the user from downloading it. I want to be able to do the same with the image if it was store in the FileSystem, but im not sure if this is possible with PHP and Files in the system.

Zoltán Tamási
  • 12,249
  • 8
  • 65
  • 93
IEnumerable
  • 3,610
  • 14
  • 49
  • 78
  • maybe also read: http://stackoverflow.com/questions/191845/how-to-store-images-in-your-filesystem – Jacco Dec 19 '11 at 12:01

1 Answers1

3

Keeping relevant file names can be good for SEO, but you must also make sure you don't duplicate.

In all cases I would rename files to lowercase and replace spaces by underscores (or hyphens)

Justin Beiber Found dead.jpg => justin_beiber_finally_dead.jpg

If the photo's belongs to an article or something specific you can perhaps add the article ID to the image, i.e. 123_justin_beiber_found_dead.jpg. Alternatively you can store the images in an article specific folder, i.e. /images/123/justin_beiber_found_dead.jpg.

Naming the files like a0000001 removes all relevance to the files and adds no value whatsoever.

Store (full) filepaths only in the database.

For part 2;

I'm not sure what the best solution here is, but using the filesystem, I think you will have to configure apache to serve all files in a particular directory by PHP. In PHP you can then check if the file can be published and then spit it out. If not, you can serve a dummy image. This however is not very efficient and will be much heavier on apache.

Richard
  • 4,341
  • 5
  • 35
  • 55
  • I think Ill leave the image security alone for now, I have implemented your above solution by keeping friendly image name and replacing the white space with underscore, I ahve also stored full image path in db so image can freely move around and not be tied down to any web page, thanks for your input. :) – IEnumerable Dec 18 '11 at 13:57
  • Comparing the naming schemes "images/123/justin.jpg" vs. "images/123_justin.jpg", is one any better than the other? – Patrick Nov 11 '16 at 22:44
  • @Patrick I would suggest using `images/123/justin.jpg` as when the file name is downloaded, it's just `justin.jpg`. As IDs increase you can end up with `465461684_justine.jpg`. – Richard Nov 14 '16 at 09:11
  • Why is it beneficial to have just `justin.jpg`? If you have a minute, could you add your answer to my question here: http://stackoverflow.com/questions/40558806/best-directory-structure-for-static-files-hierarchical-vs-flat – Patrick Nov 15 '16 at 16:33
  • Because it's relevant. Incremented numbers before a file name are irrelvant in most case, unless they are required for unique names. – Richard Nov 16 '16 at 07:06
  • poor justin bieber. This answer is also good if you consider that you may have variants of the same image such as thumb, small, large sizes. For example `123_is_justin_dead_thumb.jpg` – darkace Jun 22 '20 at 15:12