views:

505

answers:

1

Hi

I've been using the Zend Framework rather intensively to develop my website, and I'm now designing the upload interface. I need to allow the user to upload 1-5 images. I need to send these images to the database as a BLOB object. My current code is something like this:

$upload = new Zend_File_Transfer_Adapter_Http();
$upload->addValidator('Extension', true, 'jpg,png,gif');
$upload->addValidator('ImageSize', true, array(
        'minwidth' => 4)); //i preform this check to make sure that it's a real
                           //image, since IsImage only checks MIME. Good Idea?
$upload->addValidator('IsImage', false);
$upload->addValidator('Size', true, array(
        'min' => '5kB' , 
        'max' => '1MB' , 
        'bytestring' => false));
if (! $upload->receive()) {
    $messages = $upload->getMessages();
    echo implode("\n", $messages);

I have two questions.

First, what is a good, fast and secure way to confirm that the upload file is actually an image? MIME types and extentions can be easily faked. What I did adove seems foolish. Any other ideas?

Second, where do I proceed from here? I know Zend has the setDestination method, but I'm moving it to a database, not to a file.

Thanks.

+2  A: 
  1. You can use getimagesize() to check that the file is genuinely and image. (don't be fooled by its name - it can give you much more info than just the size)
  2. Use file_get_contents() to read in the contents of the image file for storing in the database.
karim79
The ImageSize validator probably calls getimagesize() so that's covered :) I agree with karim though, it should be secure enough
David Caunt