When you use image (or any file for that matter) with PHP in an application you can either store it in the DB or as a static file. We have chose static files for speed purposes and to cut down on DB size.
However, when we do this we do not know the specific filename of the file corresponding to a database record. For instance, a image file for an article which is stored in the database.
Sure we could store them according to the primary ID but then you dont know the file type (jpeg, gif, etc.).
We can easily look this up with glob() and a few other functions. But is this going to be slower and more tedious than just storing in an extra column "image_file_name" the filename like "22.gif". Or going even further and storing the path "/images/articles/22.gif".
Any suggestions on which of these you would prefer for ease of programming, speed, etc?
- lookup path
- store filename only
- store filepath and filename
I would store all details about the file in the database, filename, path, type, size, dimensions (if its an image), etc. That way when I query the database for the file I don't need to run test on the file to get the additional information if I might need it.
Logic without the fatal effects.
All code snippets are licensed under WTFPL.
I agree that storing the image as a separate file and storing the filename in the DB is a good idea.
I would recommend storing just the filename rather than the whole path. The directory path could then be a config setting that is the same for all images. That way, if you want to move to a new server (for example) you don't have to update every record in the database with the new path. The image filename would remain the same.
I would only consider storing the extra image info (size, etc) in the DB if it was going to be useful (ie regularly accessed). If this info is seldom used, it would make more sense just to ask the filesystem as required rather than take up DB space.
Note: I am assuming that when you talk about the filename you mean the sanitised version? Always beware of user-supplied filenames!