SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Addict
    Join Date
    Jan 2007
    Posts
    323
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    storing images filename or look it up with PHP?

    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

  2. #2
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    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.


  3. #3
    SitePoint Member
    Join Date
    May 2006
    Location
    Sunshine Coast Hinterland, Australia
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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!


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •