PHP-Can';无法在没有扩展名的文件上读取文件大小
到目前为止,我的filemanager脚本一直运行良好。它不知道如何处理没有扩展名的文件,而是在试图显示程序中的所有文件及其大小时抛出错误 下面是获取文件并将其添加到适当数组的调用PHP-Can';无法在没有扩展名的文件上读取文件大小,php,apache2.4,Php,Apache2.4,到目前为止,我的filemanager脚本一直运行良好。它不知道如何处理没有扩展名的文件,而是在试图显示程序中的所有文件及其大小时抛出错误 下面是获取文件并将其添加到适当数组的调用 $errors = array(); $items = array(); $folders = array(); $files = array(); $dir = $base_dir; if(is_dir($dir)) { if($dh = opendir($dir)) { while(($f
$errors = array();
$items = array();
$folders = array();
$files = array();
$dir = $base_dir;
if(is_dir($dir)) {
if($dh = opendir($dir)) {
while(($file = readdir($dh)) !== false) {
if($file == "." || $file == "..") {
continue;
} else {
$filesize = filesize($dir . "/" . $file);
$filesize = $x10->function->realFileSize($filesize);
$items[] = array(
'name' => $file,
'size' => $filesize,
'ext' => substr($file, strrpos($file, "."))
);
}
}
closedir($dh);
}
for($i = 0; $i < count($items); ++$i) {
$filename = $items[$i]['name'];
$extension = $items[$i]['ext'];
if($extension == $filename) {
if($filename == ".htaccess" || $filename == "magic") {
$files[] = $items[$i];
} else {
$folders[] = $items[$i];
}
} else {
$files[] = $items[$i];
}
}
} else {
$errors[] = "1";
}
这是用于查找文件的realFileSize的引用函数
function GetDirectorySize($path) {
$bytestotal = 0;
$path = realpath($path);
if($path !== false && $path != '' && file_exists($path)) {
foreach(new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path, FilesystemIterator::SKIP_DOTS)) as $object){
$bytestotal += $object->getSize();
}
}
return $bytestotal;
}
我找不到处理此异常的方法,因为文件
README
没有扩展名,但目录中的大小为3KB。是否有其他方法可以跳过这些文件或使其正确加载?我在这里束手无策。您是否尝试过这样的方法来捕捉错误:
try {
foreach(new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path, FilesystemIterator::SKIP_DOTS)) as $object){
$bytestotal += $object->getSize();
}
}
catch(UnexpectedValueException $e) {
// error here print $e->getMessage();
}
顺便说一下,请注意,当您使用您编写的代码时,没有扩展名的文件“zuumba”的ext将是“a”:
$items[] = array(
'name' => $file,
'size' => $filesize,
'ext' => substr($file, strrpos($file, "."))
);
我试图对它使用
try{}catch(UnexpectedValueException$e){}
,但仍然得到致命错误:Uncaught UnexpectedValueException:RecursiveDirectoryIterator::\uu构造(C:\apache\error\READ…
错误消息。尝试移动在帖子中编辑的try catch,以便将for循环放在try中…最终还将您的'ext'=>substr($file,strrpos($file,“.”)更改为以下内容:'ext'=>substr($file,((strrpos($file,”)>=0)?strrpos($file,“):0)在对象调用之前添加try-and-catch是的,但是项目上根本没有文件大小,它在排序中被注册为目录而不是文件。对我来说,您的错误消息表明,错误在这一行,而不是getsize行:foreach(新的递归迭代器(新的RecursiveDirectoryIterator($path,FileSystemEmitter::SKIP_DOTS))作为$object){
所以可能是你的$path出错了什么的……或者是行路径上的双新或跳转点或任何可能出错的东西正在加载每个文件,除了没有扩展名的文件。我找到了一个临时的解决方法,但我会继续研究一个更长期的计划。谢谢。
$items[] = array(
'name' => $file,
'size' => $filesize,
'ext' => substr($file, strrpos($file, "."))
);