如何提取文件扩展名并根据允许的扩展名数组进行检查


How to extract the filename extension and check it against an array of allowed extensions?

如何准备主题字符串以从$allowed数组中伪造preg_match检查扩展?目标是使用扩展名不在允许的数组中的文件名。我尝试了一些技巧,比如UTF8编码和新行注入,但无法实现任务目标。谢谢你,很抱歉我的英语

    $allowed = array('tst', 'dll');
$filename = basename($test);
if (preg_match('#'.(.+)$#', $filename, $matches) && isset($matches[1]) && !in_array($matches[1], $allowed))
    die("Extension not allowed!");
echo $ext;

pathinfo是您想要的

PHP.net

$file_parts = pathinfo($filename);
switch($file_parts['extension'])
{
    case "jpg":
    break;
    case "exe":
    break;
    case "": // Handle file extension for files ending in '.'
    case NULL: // Handle no file extension
    break;
}

这应该是一个很好的例子。