开发者

How to you check to see if file uploaded is a php file?

Hey i'm just wonderin how to check if the file type of uplo开发者_StackOverflow社区ad file is php.

I have tried using:

$type = $_FILES["file"]["type"];

if(!$type == ".php") {

//error
} else {

//upload
}


I'm presuming that you want something a bit more robust than checking to see if the file extension is .php? If so, then you can run the file through the PHP syntax checker (php -l mystery_file) via system().


Try this:

function getExt($fn){
    $a=pathinfo($fn);
    return $a['extension'];
}

$type = getExt($_FILES["file"]["name"]);

if($type == "php") {
//upload

} else {
//error

}


The type var is a Mime-Type, so it doesn't describe the ending like .php, it describes the type of the data conatined, like application/x-httpd-php or text/php

Just try to upload a php file and see what

echo $type;

gives you, then you'll know what to check for.


Here is the list of possible php mime type

   text/php
   text/x-php
   application/php
   application/x-php
   application/x-httpd-php
   application/x-httpd-php-source

..or you can just echo the file type out and check against it.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜