Get File Extension

本文介绍如何使用PHP函数获取文件扩展名,并提供了一种使用字符串操作的方法。此外,文章还提供了关于历史版本和现代替代方案的背景信息。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Ever needed to simply get the file extension from the end of a file? This can be done with the pathinfo() function, but what was needed was the dot also in this case. Whilst pathinfo() gives the extension name, it does not return the dot. Keep in mind this function was created in an early version of php3 and is left for historical purposes. Both methods are provided for fun.


<?php

/*** example usage ***/
$filename = 'filename.blah.txt';

/*** get the path info ***/
$info = pathinfo($filename);

/*** show the extension ***/
echo $info['extenstion'];

?>

 

This second method does basically the same as the above, but uses string manipulation to get the extension and grabs the dot(.) character also.

<?php

/*** example usage ***/
$filename = 'filename.blah.txt';

echo getFileExtension($filename);

/**
*
* @Get File extension from file name
*
* @param string $filename the name of the file
*
* @return string
*
**/
function getFileExtension($filename){
  return substr($filename, strrpos($filename, '.'));
}
?>

 



转载于:https://www.cnblogs.com/imluxin/archive/2012/04/24/2468648.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值