Search code examples
phpphotouploader

php photo uploader


I have a very simple photo uploader which needs some bringing up to speed please

Firstly an echo appears when the page loads even tho there is nothing in the box?

if($_POST['upload']) {
     if($_FILES['image']['name'] == "")
     {
         #there's no file name return an error
         echo "\n<b>Please select a file to upload!\n</b>";
         exit;
     }
     #we have a filename, continue
}

#directory to upload to
$uploads = '/home/habbonow/public_html/other/quacked/photos';
$usruploads = 'photos';

#allowed file types
$type_array = array('image/gif','image/pjpeg','image/x-png');

if(!in_array($_FILES['image']['type'], $type_array))
{
    #the type of the file is not in the list we want to allow
    echo "\n<b>That file type is not allowed!\n</b>";
    exit;
}

the page output shows the uploading box but also echos "That file type is not allowed!" even when i haven't clicked the button.

secondly what is the mime type for jpg please, as I have jpeg and pjpeg.

thanks, any help appreciated.



Solution

  • I would also suggest putting everything in the POST block, otherwise it will be evaluated when the page is loaded no matter what.

    For mimetypes there is a method image_type_to_mime_type which lets you pass in a constant representing a given filetype and returns the proper mimetype for it, e.g.:

    $type_array = array(image_type_to_mime_type(), image_type_to_mime_type(IMAGETYPE_GIF), image_type_to_mime_type(IMAGETYPE_PNG), 'image/pjpeg');
    

    (Since pjpeg doesn't have it's own constant we can just add it manually)