note 45168 modified in features.file-upload by aidan
| From: | aidan@php.net | Date: | Tue, 12 Oct 2004 08:37:27 +0000 |
| Subject: | note 45168 modified in features.file-upload by aidan | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-78396@lists.php.net to get a copy of this message | ||
If your upload script is meant only for uploading images, you can use the image function
getimagesize() (does not require the GD image library) to make sure you're really getting an
image and also filter image types.
<?php getimagesize($file); ?>
...will return false if the file is not an image or is not accessable, otherwise it will return an
array...
<?php
$file = 'somefile.jpg';
# 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";
}
?>
using this function along with others mentioned on this page, image ploading can be made pretty much
fool-proof.
See http://php.net/manual/en/function.getimagesize.php
for supported image types and more info.
--was--
If your upload script is meant only for uploading images, you can use the image function
getimagesize() (does not require the GD image library) to make sure you're really getting an
image and also filter image types.
<?php getimagesize($file); ?>
...will return false if the file is not an image or is not accessable, otherwise it will return an
array like the following...
Array
(
[0] => 750
[1] => 139
[2] => 2
[3] => width="750" height="139"
[bits] => 8
[channels] => 3
[mime] => image/jpeg
)
<?php
$file = somefile.jpg;
# 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";
break;
}
}
else {
echo "file is not a valid image file";
?>
using this function along with others mentioned on this page, image uploading can be made pretty
much fool-proof.
See http://ca2.php.net/manual/en/function.getimagesize.php
for supported image types and more info.
http://php.net/manual/en/features.file-upload.php