php通过文件路径获取文件名/文件标题/文件后缀名/文件大小

通过路径获取文件名

function getFileName($filePath) {
	return \mb_substr($filePath, \mb_strrpos($filePath, '/') + 1);
}
// V2
function getFileName($filePath = '') {
	return \strtolower(\substr(\strrchr($filePath, '/'), 1));
}

通过路径获取文件标题

function getFileTitle($filePath) {
	$fileName = \mb_substr($filePath, \mb_strrpos($filePath, '/') + 1);
	return mb_substr($fileName, 0, mb_strrpos($fileName, '.'));
}

通过路径获取文件后缀名

function getFileSuffix($filePath) {
	return \mb_substr($filePath, \mb_strrpos($filePath, '.') + 1);
}
// V2
function getFileExt($filePath = '') {
	return \strtolower(\substr(\strrchr($filePath, '.'), 1));
}

通过文件路径返回文件大小(单位:b)

function getFileSize($url) {
	$url = parse_url($url);
	if ($fp = @fsockopen($url['host'], empty($url['port']) ? 80 : $url['port'], $error)) {
		fputs($fp, "GET " . (empty($url['path']) ? '/' : $url['path']) . " HTTP/1.1\r\n");
		fputs($fp, "Host:$url[host]\r\n\r\n");
		while (!feof($fp)) {
			$tmp = fgets($fp);
			if (trim($tmp) == '') {
				break;
			} else if (preg_match('/Content-Length:(.*)/si', $tmp, $arr)) {
				return trim($arr[1]);
			}
		}
		return null;
	} else {
		return null;
	}
}
// {:number_format(getFileSize($vo)/1024,1,'.','')}kb

posted on   小馬過河﹎  阅读(351)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
点击右上角即可分享
微信分享提示