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 运动想象脑电信号数据集.vhdr
  • ¥15 三因素重复测量数据R语句编写,不存在交互作用
  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目