0

I am working on an app where users can upload images and iv'e been setting it up on Google App Engine which i think is fantastic so far. But I'm having trouble figuring our the best way to validate a users upload as a proper image, I cant see anything in their documentation about it and the search results on Google are very few and far between so i'm wondering if i am headed down the wrong path with this.

Basically I don't want to store files that aren't images (cost and consistency reasons mainly as well as protecting my users) so I need a valid way to determine that, I was thinking of using the cloud storage tools API to get the content type but i'm wondering if that is just going to be based off the file extension, because the file type that the GCS upload url gave me back was a 'image/jpeg' when i renamed a .exe to a .js

I really feel like i'm missing something here, has anyone else come across this issue on Google app engine yet?

KrizOne
  • 29
  • 6
  • 1
    An alternate method to the answer and dependent on how paranoid you are. Use the image service to generate thumbnails from the images. If this is successful then in theory they are images. However the appengine image service is not available for php. – Tim Hoffman Apr 30 '14 at 03:14
  • This is a great idea, my friend also mentioned that it wasn't available in PHP but I recently created a image serving URL with a cropped thumbnail size using GCS Tools in PHP with no hassles at all, perhaps it has recently been implemented? Thanks very much for your advice :) – KrizOne Apr 30 '14 at 03:27

4 Answers4

1

To determine the file type, you will need a mechanism to inspect the first few bytes of the file that will typically contain the magic number. The magic number is a way to determine the file type. Take a look at various magic numbers for popular file extensions : http://billatnapier.wordpress.com/2013/04/22/magic-numbers-in-files/

Romin
  • 8,708
  • 2
  • 24
  • 28
  • I had no idea that this was a thing, thank you very much! I think it might be a bit easier for me to feed it through the image tools on GCS but this certainly looks like a good way to achieve an answer on what kind of file it is. If i have no success with the GCS Image tools ill let you know how this goes! Thanks! :) – KrizOne Apr 30 '14 at 03:29
1

The GD extension is available, so you could call getimagesize on the file.

If it gives you a valid size then assume it's an image, otherwise it is not.

Stuart Langley
  • 7,044
  • 1
  • 20
  • 20
  • This is also something that I hadn't thought of! Thank you! This seems to be the most simple solution. – KrizOne Apr 30 '14 at 03:34
0

Similar question with solution:

Community
  • 1
  • 1
olituks
  • 487
  • 3
  • 11
0

Use exif_imagetype. Based on the docs, Its faster than getimagesize.

user229044
  • 232,980
  • 40
  • 330
  • 338