0

I have never populated a database with images before now. I will tackle the actual upload script when I come to it but for now I am creating the table in phpMyAdmin and cannot see any relevant data type.

Is this even the correct way of going about it, or would it be better to store images in the filesystem and just store the path, or filename in the database? If doing that, should I do something to the filename to 'clean' it, e.g. rename all files with a random string.

Thanks

James
  • 57
  • 2
  • 10
  • 2
    Your latter thoughts are what I'd do; store the filename and a random string into your database, and when you store the file, name it as that random string. – Chris Forrence Feb 04 '14 at 17:26
  • http://stackoverflow.com/questions/6313969/phpstore-image-into-mysql-blob-good-or-bad – Works On Mine Feb 04 '14 at 17:26
  • Yes, please go with the second thought. In the database I would store the fileName as uploaded by the user, a unique fileName generated by a script or auto-incremented ID, full server filePath, public filePath which would allow URL access, fileType, fileSize, uploadDateTime. When storing the file in your file system then rename it to the auto-incremented ID. – MonkeyZeus Feb 04 '14 at 17:30
  • @MonkeyZeus is all that really necessary? public file path can surely be hardcoded into the script that is pulling the images up, and i would have thought file size/type and other metadata could be gleaned from the file itself using some sort of function, although in my case I can't foresee me needing to. – James Feb 04 '14 at 17:35
  • To store files in your database you need to set the appropriate data type. See reference manual about the blob datatype: http://dev.mysql.com/doc/refman/5.6/en/blob.html –  Feb 04 '14 at 17:40
  • Every programming question has one universal answer: `it depends...`. Will you ever run out of storage? Do you have multiple storage locations available to you? Will all files from today, tomorrow, and 365 days from now fit comfortably on the one-single hard drive you plan to store them on? If you do have a multi-location storage solution then how is your URL going to be hard-coded to handle that? – MonkeyZeus Feb 04 '14 at 17:44
  • And yes you *can* glean at the file info such as size and type but that code needs to be executed EVERY SINGLE time the file is viewed, is all that wasted CPU time really necessary? Store it once in the DB and the CPU never has to perform that mundane task ever again for that file. – MonkeyZeus Feb 04 '14 at 17:49

2 Answers2

0

First you will need to upload file in any predefined folder and than you can store name of that file in your database(with is varchar data type).

And when you will fetch that record, use that file name to display images wherever you need to display.

Abhijeet Dange
  • 133
  • 1
  • 11
  • So when it comes to my PHP script, the code would have to perform the following functions: 1)upload&store file in designated folder 2)rename file with random string 3)store random string(filename) in database. Correct? – James Feb 04 '14 at 17:37
  • Yes correct and use that name to recreating image path wherever application required. – Abhijeet Dange Feb 05 '14 at 05:25
0

There have been several answers explaining how to store the path to the actual image in your database while keeping the file itself on disk. That is how I would do it as well, but MySQL allows you to store that as a BLOB data type (or subtypes like TINYBLOB, LONGBLOB, etc). phpMyAdmin has a feature called transformations that then allow you to view a preview of that image directly from phpMyAdmin (or other interesting display manipulations for various data types), which is kind of neat.

Again, if it were me, I'd also store it on disk as well, I'm just answering the question you asked about what data type you'd use.

Isaac Bennetch
  • 11,830
  • 2
  • 32
  • 43