我需要驗證字符串是否爲圖片文件名。php - 檢查字符串是否以圖片擴展名結尾
$aaa = 'abskwlfd.png';
if ($aaa is image file) {
echo 'it's image';
else {
echo 'not image';
}
我該怎麼做?它會拍攝100張圖像,所以它應該很快。我知道有一種文件類型驗證方法,但我認爲這很慢.. preg_match怎麼樣?速度更快嗎? 我不擅長preg_match。
預先感謝您。
我需要驗證字符串是否爲圖片文件名。php - 檢查字符串是否以圖片擴展名結尾
$aaa = 'abskwlfd.png';
if ($aaa is image file) {
echo 'it's image';
else {
echo 'not image';
}
我該怎麼做?它會拍攝100張圖像,所以它應該很快。我知道有一種文件類型驗證方法,但我認爲這很慢.. preg_match怎麼樣?速度更快嗎? 我不擅長preg_match。
預先感謝您。
試試這個:
<?php
$supported_image = array(
'gif',
'jpg',
'jpeg',
'png'
);
$src_file_name = 'abskwlfd.PNG';
$ext = strtolower(pathinfo($src_file_name, PATHINFO_EXTENSION)); // Using strtolower to overcome case sensitive
if (in_array($ext, $supported_image)) {
echo "it's image";
} else {
echo 'not image';
}
?>
它不適用於「abskwlfd.PNG」。應該使用'strtolower'來確保不區分大小寫。 – invisal
@invisal感謝您指出,在回答中添加了「strtolower」 –
是的,正則表達式是要走的路。或者,您可以圍繞"."
進行拆分,並根據圖像擴展名數組檢查返回數組中的最後一個元素。我不是一個PHP的傢伙,所以我不能給你寫的代碼,但是我可以寫正則表達式:
^[a-zA-Z\.0-9_-]+\.([iI][mM][gG]|[pP][nN][gG]|etc....)$
這是一個相當簡單的。我知道你沒有用正則表達式太多的經驗,但在這裏就是這樣一個作用:
^: start of string
[a-zA-Z\.0-9_-]: describes range of characters including all letters, numbers, and ._-
\.: "." character
([iI][mM][gG]|[pP][nN][gG]|etc....): | means or. So just put all image extensions you know here. Again, the brackets for case-insensitivity
,如果你想匹配任何序列,然後,而不是在支架和+的東西,只需使用:
.*
「。」匹配任何字符,「*」表示它的任何數量。所以這只是基本上說「沒有限制」(除了換行符)
可能有很多其他的東西我錯過了,你可以在評論中看到。只要閱讀這些,看看正則表達式的參考,你會沒事的。
一個小錯誤:「您可以分割」'。「'並檢查**最後一個元素**」 – invisal
文件名不限於'[ a-zA-Z \ .0-9 _-]'... –
確實如此,但它是與操作系統相關的。那些幾乎肯定會存在於每個操作系統中的。 – 2013-08-07 05:43:29
試試這個
$ext = pathinfo($file_name, PATHINFO_EXTENSION); case sensitive
if (in_array($ext, $supported_image)) {
echo "it's image";
} else {
echo 'not image';
}
誰說這是一個上傳?這樣寫的方式,圖像將顯示它沒有的每個文件擴展名的錯誤。 –
試試這個:
$a=pathinfo("example.exe");
var_dump($a['extension']);//returns exe
非常簡單,但我如何使用它來if語句? – newworroo
試試這個
$allowed = array(
'.jpg',
'.jpeg',
'.gif',
'.png',
'.flv'
);
if (!in_array(strtolower(strrchr($inage_name, '.')), $allowed)) {
print_r('error message');
}else {
echo "correct image";
}
或strrchr需要ch的最後一次出現字符串.. 其他一些其他的概念。
$allowed = array(
'image/jpeg',
'image/pjpeg',
'image/png',
'image/x-png',
'image/gif',
'application/x-shockwave-flash'
);
if (!in_array($image_name, $allowed)) {
print_r('error message');
}else {
echo "correct image";
}
在這裏你可以使用STRTOLOWER功能,並且也使用in_array功能
試試這個代碼,
if (preg_match('/(\.jpg|\.png|\.bmp)$/i', $aaa)) {
echo "image";
} else{
echo "not image";
}
這非常乾淨,但我意識到preg_match比使用pathinfo進行多個圖像處理的方法要慢。 – newworroo
也許您正在尋找這樣的:
function isImageFile($file) {
$info = pathinfo($file);
return in_array(strtolower($info['extension']),
array("jpg", "jpeg", "gif", "png", "bmp"));
}
pathinfo
來檢索有關文件的詳細信息,包括擴展名。strtolower
,以保證擴展將符合我們所支持的圖像列表,甚至是在不同的情況下in_array
檢查文件的擴展名是否在我們的形象extenion的列表。
另請參見:[PHP我如何檢查一個文件是MP3還是圖像文件?](http://stackoverflow.com/questions/2006632/php-how-can-i-check-if-a - 文件是MP3或圖像文件)或[如何檢查上傳的文件是否是一個沒有MIME類型的圖像?](http://stackoverflow.com/questions/6484307/how-to-check-if -an-uploaded-file-is-an-image-without-mime-type) –