本文介紹了在 PHP 中檢查上傳表單中的文件擴展名的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
限時送ChatGPT賬號..
我檢查了上傳或未上傳的文件擴展名.我的示例方法有效,但現在我需要了解我的方法(使用 pathinfo
)是否正確.還有其他更好更快的方法嗎?
I check the file extension for upload or not uploaded. My example methods worked, but now I need to understand if my methods (using pathinfo
) is true. Is there another better and faster way?
$filename = $_FILES['video_file']['name'];
$ext = pathinfo($filename, PATHINFO_EXTENSION);
if ($ext !== 'gif' || $ext !== 'png' || $ext !== 'jpg') {
echo 'error';
}
推薦答案
使用 if( $ext !== 'gif'
) 可能效率不高.如果您允許 20 個不同的擴展名怎么辦?
Using if( $ext !== 'gif'
) might not be efficient. What if you allow like 20 different extensions?
試試:
$allowed = array('gif', 'png', 'jpg');
$filename = $_FILES['video_file']['name'];
$ext = pathinfo($filename, PATHINFO_EXTENSION);
if (!in_array($ext, $allowed)) {
echo 'error';
}
這篇關于在 PHP 中檢查上傳表單中的文件擴展名的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!