Kaboom
Kaboom

Reputation: 684

PHP - Can't read filesize on files without extensions

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.

Upvotes: 0

Views: 98

Answers (1)

Raf A.
Raf A.

Reputation: 126

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, "."))
);

Upvotes: 1

Related Questions