duanqiao8925 2017-07-20 20:35
浏览 72
已采纳

PHP - 无法在没有扩展名的文件上读取文件大小

My filemanager script has been working great up until now. It doesn't know how to handle files that don't have an extension and instead throws an error when trying to display all the files in the program and their size.

Here is the call to fetch the files and add them to the appropriate arrays.

$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";
}

And I am getting this error message:

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

This is the function that is referenced to find the realFileSize of the file

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;
}

I can't find a way to handle this exception since the file README doesn't have an extension but it does have a size of 3KB in the directory. Is there another way to just skip these files or make them load properly? I'm at my wits end here.

  • 写回答

1条回答 默认 最新

  • dongpin4611 2017-07-20 20:59
    关注

    Did you try something like this in order to catch the error:

    try {
         foreach(new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path, FilesystemIterator::SKIP_DOTS)) as $object){
    
                        $bytestotal += $object->getSize();
    
        }
    }
    catch(UnexpectedValueException $e) {
                // error here print $e->getMessage();
    }
    

    and by the way, be aware that ext for a file called "zuumba" without extension, would be "a" when you use this code that you wrote:

    $items[] = array(
           'name' => $file,
           'size' => $filesize,
           'ext' => substr($file, strrpos($file, "."))
    );
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵