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 2022-09-08 17:06  小馬過河﹎  阅读(371)  评论(0)    收藏  举报

导航