2024-08-22 14:44:21 | 我爱编程网
我给你思路吧
首先没有后缀,那只能通过文件头来判断了
function getFileType($file){
if(!empty($file)){
//打开文件
$filehead = fopen($file,'r');
//读取文件2字节
$bin = fread($filehead, 2);
fclose($filehead);
//二进制字符串对数据进行解包
$data = unpack('C2chars', $bin);
$type_code = intval($data['chars1'].$data['chars2']);
switch ($type_code) {
case 7790:
$fileType = 'exe';
break;
case 7784:
$fileType = 'midi';
break;
case 8075:
$fileType = 'zip';
break;
case 8297:
$fileType = 'rar';
break;
case 255216:
$fileType = 'jpg';
break;
case 7173:
$fileType = 'gif';
break;
case 6677:
$fileType = 'bmp';
break;
case 13780:
$fileType = 'png';
break;
default:
$fileType = 'unknown';
break;
}
return $fileType;
}
-
我爱编程网(https://www.52biancheng.com)小编还为大家带来PHP中glob()函数查找指定后缀名的文件的相关内容。
建议参考以下代码
A simple function that find all files by extension an return it by an array.
<?php
function findFiles($directory, $extensions = array()) {
function glob_recursive($directory, &$directories = array()) {
foreach(glob($directory, GLOB_ONLYDIR | GLOB_NOSORT) as $folder) {
$directories[] = $folder;
glob_recursive("{$folder}/*", $directories);
}
}
glob_recursive($directory, $directories);
$files = array ();
foreach($directories as $directory) {
foreach($extensions as $extension) {
foreach(glob("{$directory}/*.{$extension}") as $file) {
$files[$extension][] = $file;
}
}
}
return $files;
}
var_dump(findFiles("C:", array (
"jpg",
"pdf",
"png",
"html"
)));
?>
把C:改成你需要查找的路径,里面放后缀,测试可用。
以上就是我爱编程网小编为大家带来的内容了,想要了解更多相关信息,请关注我爱编程网。更多相关文章关注我爱编程网:www.52biancheng.com2025-02-01 20:24:39
2025-02-12 03:21:37
2025-02-10 15:19:48
2025-01-28 17:58:32
2024-11-22 05:08:01
2024-09-10 08:50:00