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 ))
{
copy ($_FILESї'imagefile']ї'tmp_name'], "./users/".$mybbuserї'username']."/".$_FILESї'imagefile']ї'name'])
}
if ($action == "select"){
?>
<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
}
?>
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, {$_FILESї'imagefile']})) {
print("Okay, it's a .jpg.");
// do whatever
} else {
//check for .gif and others and then:
print("Wrong kind of file");
}
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ї'imagefile']ї'type'],$allow_types))
{
//upload, etc etc
}
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.