Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/277.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 获取文件扩展名_Php - Fatal编程技术网

Php 获取文件扩展名

Php 获取文件扩展名,php,Php,我正在“.”上爆炸以获取文件格式和名称: list($txt, $ext) = explode(".", $name); 问题是有些文件的名称带有点 如何在最后一个“.”上展开,以便从以下位置获得pic.n2.jpg和$ext=jpg?使用PHP的pathinfo()函数 请参阅此处的更多信息 例如: echo $file_part['extension']; echo $file_part['filename']; 输出: php 123.测试使用: 使用这个 $array = explo

我正在“.”上爆炸以获取文件格式和名称:

list($txt, $ext) = explode(".", $name);
问题是有些文件的名称带有点

如何在最后一个“.”上展开,以便从以下位置获得
pic.n2.jpg
$ext=jpg

使用PHP的pathinfo()函数

请参阅此处的更多信息

例如:

echo $file_part['extension'];
echo $file_part['filename'];
输出:

php 123.测试使用:

使用这个

$array = explode(".", $name);
end($array);         // move the internal pointer to the end of the array
$filetype = current($array);

谢谢

您可以将自己的函数编写为

函数getExtension($str){

$i=strrpos($str,“.”)

如果(!$i){返回“”;}

$l=strlen($str)-$i

$ext=substr($str,$i+1,$l)

返回$ext

}


使用Pathinfo或mime内容类型获取文件类型信息

$filetype = pathinfo($file, PATHINFO_FILENAME);

$mimetype = mime_content_type($file);

您可以尝试以下方法:

<?php
$file = 'a.cool.picture.jpg';

$ext = substr($file, strrpos($file, '.')+1, strlen($file)-strrpos($file, '.'));
$name = substr($file, 0, strrpos($file, '.'));
echo $name.'.'.$ext;
?>

关键函数是strrpos()和substr(),前者查找最后出现的字符(本例中为“.”),后者返回子字符串。在文件中找到最后一个“.”,并将其子字符串化。希望有帮助。


<?php
$path = 'http://www.mytest.com/public/images/portfolio/i-vis/abc.y1.jpg';
echo $path."<br/>";
$name = basename($path);
$dir = dirname($path); 
echo $name."<br/>";
echo $dir."<br/>";
$pi = pathinfo($path);
$txt = $pi['filename']."_trans";
$ext = $pi['extension'];
echo $dir."/".$txt.".".$ext;
?>

最好使用上述解决方案之一,但也有一种使用explode功能的解决方案:

$filename = "some.file.name.ext";
list($ext, $name) = explode(".", strrev($filename), 2);
$name = strrev($name);
$ext = strrev($ext);
此解决方案的作用如下:
1.反转字符串,使其看起来像:txe.eman.elif.emos
2.分解它,您将得到类似:$ext=“txe”、$name=“eman.elif.emos”

3.反转每个变量以获得正确的结果

您使用的是本地文件还是随机文本?我也喜欢explode函数,尽管获取文件名需要循环或内爆,但这更切题。(尽管代码凌乱,但这是理所当然的。)您不必向
substr
提供第三个参数,
substr($file,strrpos($file,,)+1)
单独就可以找到扩展名。还有
current
函数,而不是
获取当前元素的迂回方式。
$filetype=end($array)也可以工作。不需要最后一行。太棒了,比我的解决方案优雅多了
<?php
$file = 'a.cool.picture.jpg';

$ext = substr($file, strrpos($file, '.')+1, strlen($file)-strrpos($file, '.'));
$name = substr($file, 0, strrpos($file, '.'));
echo $name.'.'.$ext;
?>
<?php
$path = 'http://www.mytest.com/public/images/portfolio/i-vis/abc.y1.jpg';
echo $path."<br/>";
$name = basename($path);
$dir = dirname($path); 
echo $name."<br/>";
echo $dir."<br/>";
$pi = pathinfo($path);
$txt = $pi['filename']."_trans";
$ext = $pi['extension'];
echo $dir."/".$txt.".".$ext;
?>
$filename = "some.file.name.ext";
list($ext, $name) = explode(".", strrev($filename), 2);
$name = strrev($name);
$ext = strrev($ext);