PHPでファイル拡張子を取得
1686 ワード
PHP N
, :
1 :
function get_extension($file)
{
substr(strrchr($file, '.'), 1);
}
2 :
function get_extension($file)
{
return substr($file, strrpos($file, '.')+1);
}
3 :
function get_extension($file)
{
return end(explode('.', $file));
}
4 :
function get_extension($file)
{
$info = pathinfo($file);
return $info['extension'];
}
5 :
function get_extension($file)
{
return pathinfo($file, PATHINFO_EXTENSION);
}
, , 1、2 , pathinfo 。 , 。 , 。
., /home/test.d/test.txt
., 。 /home/test.d/test
:1、2 ,3 。4 , , 。 5 。 pathinfo 。 :
$file_path = pathinfo('/www/htdocs/your_image.jpg');
echo "$file_path ['dirname']
";
echo "$file_path ['basename']
";
echo "$file_path ['extension']
";
echo "$file_path ['filename']
"; // only in PHP 5.2+
, , , , extension , 4 。 phpinfo 。 , :
PATHINFO_DIRNAME -
PATHINFO_BASENAME - ( )
PATHINFO_EXTENSION -
PATHINFO_FILENAME - ( ,PHP>5.2)
1、2、4、8, :
pathinfo($file, PATHINFO_EXTENSION | PATHINFO_FILENAME);
, 。 1 。