我检查文件扩展名是否已上传。我的示例方法有效,但现在我需要了解我的方法(使用 pathinfo
)是否正确。还有其他更好更快的方法吗?
$filename = $_FILES['video_file']['name'];
$ext = pathinfo($filename, PATHINFO_EXTENSION);
if ($ext !== 'gif' || $ext !== 'png' || $ext !== 'jpg') {
echo 'error';
}
最佳答案
使用 if( $ext !== 'gif'
) 可能效率不高。如果您允许 20 种不同的扩展程序怎么办?
试试:
$allowed = array('gif', 'png', 'jpg');
$filename = $_FILES['video_file']['name'];
$ext = pathinfo($filename, PATHINFO_EXTENSION);
if (!in_array($ext, $allowed)) {
echo 'error';
}
关于php - 在 PHP 中检查上传表单中的文件扩展名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10456113/