Page 1 of 1

Upload just images?

Posted: Wed Mar 02, 2005 6:50 pm
by Dale
I'm using this basic uploader, but how do configure it to allow just GIF, JPG, JPEG & PNG images?

Code: Select all

<?php
if(isset( $Submit ))
&#123;
copy ($_FILES&#1111;'imagefile']&#1111;'tmp_name'], "./users/".$mybbuser&#1111;'username']."/".$_FILES&#1111;'imagefile']&#1111;'name'])
&#125;

if ($action == "select")&#123;
?>
<form name="form1" action="" method="POST" enctype="multipart/form-data"><input type="file" name="imagefile" size="50"><br><br><input type="submit" name="Submit" value="Upload Image"></form>
<?php
&#125;
?>

Posted: Wed Mar 02, 2005 6:57 pm
by cbrian
You'd have to use Regular expressions, i think. Something like

Code: Select all

$pattern = "\.jpg$";
if(eregi($pattern, &#123;$_FILES&#1111;'imagefile']&#125;)) &#123;
print("Okay, it's a .jpg.");
// do whatever
&#125; else &#123;
//check for .gif and others and then:
print("Wrong kind of file");
&#125;
I think it would be something like that. The above code probably wouldn't work. It's just an example.

Posted: Wed Mar 02, 2005 6:59 pm
by John Cartwright

Code: Select all

$allow_types = array ('image/gif', 
                                 'image/x png', 
                                 'image/jpeg', 
                                 'image/tiff', 
                                 'image/x-ms-bmp', 
                                 'image/bmp', 
                                 'image/pjpeg'
);

if (in_array($_FILES&#1111;'imagefile']&#1111;'type'],$allow_types))
&#123;
      //upload, etc etc
&#125;
Not really neccesary to use regex engines

Posted: Wed Mar 02, 2005 8:00 pm
by shiznatix
only way to be certain its even a image is getimagesize(); then do the array stuff.

Posted: Wed Mar 02, 2005 8:41 pm
by smpdawg
<?php
$file
= $_FILES['imagefile']['tmp_name'];

# assuming you've already taken some other
# preventive measures such as checking file
# extensions...

$result_array = getimagesize($file);

if (
$result_array !== false) {
   
$mime_type = $result_array['mime'];
   switch(
$mime_type) {
       case
"image/jpeg":
           echo
"file is jpeg type";
           break;
       case
"image/gif":
           echo
"file is gif type";
           break;
       default:
           echo
"file is an image, but not of gif or jpeg type";
   }
} else {
   echo
"file is not a valid image file";
}
?>
This example came from PHP.net and addresses the points that have been mentioned.

Posted: Wed Mar 02, 2005 8:42 pm
by John Cartwright
shiznatix wrote:only way to be certain its even a image is getimagesize(); then do the array stuff.
Thats correct, feyd discussed this erlier explaining how getimagesize cannot be tricked by file extension changing and such.. pretty good.