-2

可能重复:
如何只允许在 php 中上传某些文件类型?

我正在学习 PHP 和 Mysql。我想创建一个只允许用户上传的特定文件扩展名的函数。喜欢..

$whitelist = array("jpg","jpeg","gif","png","bmp","pdf" );

那么我如何使用 php.ini 检查这个特定的文件扩展名。谁能告诉我。。谢谢。

4

3 回答 3

2

这是一个选项:

function checkFormat($source){
    $whitelist = array("jpg","jpeg","gif","png","bmp","pdf" );
    $type="";
    if(version_compare('4.3.0', PHP_VERSION) > 0){
        $type=mime_content_type($source['tmp_name']);
    }else{
        $type=$source['type'];
    }

    return (in_array($type, $whitelist));
}

问候!

于 2012-04-09T08:01:13.763 回答
2

Get the file extension of the uploaded file with pathinfo­Docs like

$extension = pathinfo($filename, PATHINFO_EXTENSION);

and then check against a whitelist of allowed exentsions

$allowedExtensions = array("jpg","jpeg","gif","png","bmp","pdf");
$isAllowed = in_array($extension, $allowedExtensions);

maybe you need to modify the extension recognize function to get some special extensions like tar.gz, but that should do the trick.

Edit: As said here you shouldn't check the file extension, but the mime-type.

于 2012-04-09T08:02:09.827 回答
0

看这里: http ://www.w3schools.com/php/php_file_upload.asp

它提供了一个示例来说明如何执行此操作。

于 2012-04-09T08:07:21.617 回答