Upload just images?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Dale
Forum Contributor
Posts: 466
Joined: Fri Jun 21, 2002 5:57 pm
Location: Atherstone, Warks

Upload just images?

Post 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;
?>
cbrian
Forum Commoner
Posts: 97
Joined: Sun Feb 27, 2005 12:29 pm

Post 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.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post 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
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Post by shiznatix »

only way to be certain its even a image is getimagesize(); then do the array stuff.
User avatar
smpdawg
Forum Contributor
Posts: 292
Joined: Thu Jan 27, 2005 3:10 pm
Location: Houston, TX
Contact:

Post 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.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post 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.
Post Reply