0

I am building a website where I am uploading images to my ftp folder through PHP script. Now I want to display those images on to my HTML pages. I was thinking about using PHP and getting array of all the images from my ftp folder and then display them using image view.

Please tell me if I am doing this the wrong way and if there is any other better alternatives to it. I was reading php manual for ftp_nlist and ftp_rawlist but did not understand.

j08691
  • 204,283
  • 31
  • 260
  • 272
user1704671
  • 19
  • 1
  • 7

1 Answers1

0

Well it may depend on how many images you have in there. Probably the most "correct" way to do it would be to store the filenames in a DB. You could scan the entire folder, but for every single request that's potentially a lot of overhead rather than just grabbing them out of a DB. Are you manually uploading the images? Give us more details on how that works and we can better serve you. If you're using a script to upload images (I've had lots of projects where that's the case), then you can just have the script insert those filepaths into the DB for you. If not, (you're manually uploading them), or if indeed there are not a large number of files, then scanning the folder wouldn't necessarily be a bad thing. I've used that method on smaller projects myself.

Read up on the php readdir function in the docs (which actually works a lot like mysql_fetch_assoc, ironically)- That will provide you with an excellent way to go without setting up a DB. For an approach where an upload script handles it, I recommend a DB. Without more info, it's hard to say. Good luck!

dudewad
  • 476
  • 4
  • 8
  • thanks for your response I do have upload script running on my website which allows you to upload images and than saves them in to my upload directory. Now how can I display those uploaded images on my webpage. I wanted to stay away from DB. – user1704671 Sep 28 '12 at 17:28
  • Yeah- let me know if it works for you, and vote the answer as accepted so other people with the same question get helped out by it :) – dudewad Sep 28 '12 at 17:30
  • Never store images in a database – Cole Tobin Sep 28 '12 at 17:45
  • @ColeJohnson - Why not? We're not talking about storing the image as binary. We're talking about storing the filepath to the image...? Quite honestly, in the majority of cases when you have data lookup like this it should be stored in a DB as far as I'm concerned. – dudewad Sep 28 '12 at 17:50
  • @dudewad Storing the path is fine, but storing the entire thing is a huge perf killer as when you add a new entry to the database, the entire file is restructured which uses loads of disk activity. You may want to [look at this](http://stackoverflow.com/questions/766048/store-image-in-database-or-in-a-system-file) – Cole Tobin Sep 29 '12 at 00:09
  • @ColeJohnson Converting and storing the entire image wasn't part of this discussion, though I suppose that since I didn't explicitly say "store the filepaths" that it could confuse less experienced devs. Though, typically, when you say "store something in a database" you're talking about storing a reference, not the asset itself, which is very rare and usually something one would specify. I edited the original response to reflect. – dudewad Sep 29 '12 at 18:27