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 能给我一些人生建议吗
  • ¥15 mac电脑,安装charles后无法正常抓包
  • ¥18 visio打开文件一直显示文件未找到
  • ¥15 请教一下,openwrt如何让同一usb储存设备拔插后设备符号不变?
  • ¥30 使用quartz框架进行分布式任务定时调度,启动了两个实例,但是只有一个实例参与调度,另外一个实例没有参与调度,不知道是为什么?请各位帮助看一下原因!!
  • ¥50 怎么获取Ace Editor中的python代码后怎么调用Skulpt执行代码
  • ¥30 fpga基于dds生成幅值相位频率和波形可调的容易信号发生器。
  • ¥15 R语言shiny包和ncdf4包报错
  • ¥15 origin绘制有显著差异的柱状图和聚类热图
  • ¥20 simulink实现滑模控制和pid控制对比,提现前者优势