PHP - 无法读取没有扩展名的文件的文件大小
PHP - Can't read filesize on files without extensions
到目前为止,我的文件管理器脚本运行良好。它不知道如何处理没有扩展名的文件,而是在尝试显示程序中的所有文件及其大小时抛出错误。
这里是获取文件并将它们添加到适当数组的调用。
$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";
}
我收到此错误消息:
Fatal error: Uncaught UnexpectedValueException: RecursiveDirectoryIterator::__construct(C:\apache\error\README,C:\apache\error\README): The directory name is invalid. (code: 267) in C:\panel\htdocs\core\functions.php:318 Stack trace: #0 C:\panel\htdocs\core\functions.php(318): RecursiveDirectoryIterator->__construct('C:\apache\error...', 4096) #1 C:\panel\htdocs\filemanager.php(229): Functions->GetDirectorySize('C:\apache\error...') #2 {main} thrown in C:\panel\htdocs\core\functions.php on line 318
这是查找文件真实文件大小所引用的函数
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, "."))
);
到目前为止,我的文件管理器脚本运行良好。它不知道如何处理没有扩展名的文件,而是在尝试显示程序中的所有文件及其大小时抛出错误。
这里是获取文件并将它们添加到适当数组的调用。
$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";
}
我收到此错误消息:
Fatal error: Uncaught UnexpectedValueException: RecursiveDirectoryIterator::__construct(C:\apache\error\README,C:\apache\error\README): The directory name is invalid. (code: 267) in C:\panel\htdocs\core\functions.php:318 Stack trace: #0 C:\panel\htdocs\core\functions.php(318): RecursiveDirectoryIterator->__construct('C:\apache\error...', 4096) #1 C:\panel\htdocs\filemanager.php(229): Functions->GetDirectorySize('C:\apache\error...') #2 {main} thrown in C:\panel\htdocs\core\functions.php on line 318
这是查找文件真实文件大小所引用的函数
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, "."))
);