Mirage
Mirage

Reputation: 31548

Which is best way to store uploaded images in php website

I am making the small finance management website in php. I have to store quite a few images for my application but i am confused where should i store them. My Scenario is

The user will have

  1. Saving Account
  2. Fixed Deposit

Now for Every Fixed Deposit i need to store the

  1. scanned image of that FD
  2. one or more images for source of money like Bank Cheques

Now where should i store all those images. I mean

  1. Do I need to create folder for each user then each FD and then sources
  2. Or All images in one folder

Upvotes: 0

Views: 1565

Answers (2)

NightHawk
NightHawk

Reputation: 3681

I would definitely have a folder (user ID) per user, so you won't have to parse a giant folder of images if you're trying to load one user's images. Beyond that, I think you could just store them in a single folder and use a naming convention or group all images in a sub-folder in a way that makes the retrieval of those images easier.

Upvotes: 0

Bob Baddeley
Bob Baddeley

Reputation: 2262

Don't store all the images in one folder; you'll have issues once you have thousands of files in the same folder. It would make sense and divide up nicely to create a subfolder for each user.

Don't put the folder in a web accessible directory. Instead have an intermediary php script that accepts some parameters, checks whether the requesting user has privileges to view the file, and then use readfile() to return the image. That way you prevent people from viewing other people's images.

Don't use the file name of the uploaded image. Instead you could use the id of database row as the name, or create a UUID; something unique and generated by you. That way you don't have to worry about users uploading files with conflicting names or attempting to hack your system with crazy file names.

Upvotes: 3

Related Questions