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, "."))
    );
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的