Akhil F
Akhil F

Reputation: 7740

Tricky image injection issue

I need to build some client side code which follows this use case:

  1. An image is uploaded via ftp with a name like "1.jpeg" or "1.png" (the "1" is based on the id set in the database)
  2. I need to inject an img onto the page with a src attribtue of "1.jpeg" or "1.png" (depending on which file actually exists)

My current idea is to create another spot into phpMyAdmin to dictate file type, however I'd like to avoid that (because it depends on non-programmers/designers correctly identifying filetypes). Any thoughts?

Upvotes: 0

Views: 233

Answers (1)

Alconja
Alconja

Reputation: 14873

If the client gets no more information than the id, then you've only got three options as far as I see:

  1. Have the client try each of the possible URLs, ignoring 404s until you hit a 200 response and then use that (pretty nasty solution).
  2. Write a server side handler that allows you to pass the id back and have it do the file existence checks and stream back the file. So you'd end up with something like <img src='/getimage?id=1' /> (much nicer solution, but isn't pure javascript which you imply is what you want).
  3. Pass more info back to the client in the first place (like the filetype solution you suggest, although you could probably determine it off the file rather than adding another field if you have control over the upload)

Upvotes: 1

Related Questions