Storing image/data in MySQL and naming conventions

2019-09-02 23:01发布

问题:

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.

回答1:

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.