PHP: Image Check Extension
I am currently having some problems with my upload script. It does not check if the filetype is allowed or not, and i can't figure out why. Does anyone see my problem?
//Reads the image name
$image=$_FILES['image']['name'];
//If empty
if ($image == "") {
echo '<div class="box_square form_error" style="margin-bottom:15px; padding:10px; font-size:11px;">Velg et bilde!</div>';
$errors = 1;
}
//If not empty
if ($image)
{
//Get orgiginal name from client machine
$filename = stripslashes($_FILES['image']['name']);
//Get the extension
$extension = getExtension($filename);
开发者_开发问答 $extension = strtolower($extension);
//If file extension not known, error. If not, continue
if (($extension != "jpg") && ($extension != "jpeg"))
{
//Error message
echo '<div class="box_square form_error" style="margin-bottom:15px; padding:10px; font-size:11px;">Bare JPG og JPEG er tilatte filtyper!</div>';
$errors = 1;
}
It looks like an incorrect getExtension($filename). However, i don't know why you are using stripslashes.
How about
echo "the extension used is ".$extension."<br>";
after
$extension = strtolower($extension);
btw, you can extract the extension using expression regular
preg_match("/\.([^\.]+)$/", $filename, $extension);
Do NOT rely on the extension but on the mime type instead.
You're counting on the user uploading a file with the proper extension. An alternative is getimagesize() it will also tell you the Mime type. http://us3.php.net/manual/en/function.getimagesize.php
list ( $width, $height, $type, $html_dim ) = getimagesize ($_FILES['image']['tmp_name']);
echo "\$type:" . var_dump($type);
I also see exif-imagetype() says it will do that too (but I've never used it). http://us3.php.net/manual/en/function.exif-imagetype.php
精彩评论