Jump to content

Getting file sizes for files in another directory


Zanzibarjones

Recommended Posts

I have a simple php page setup to display the files in a given directory. For the root folder it works fine. But as soon as I change directories I get:

Warning: filesize(): stat failed for blahblahblah on line blah

 

and of course. not file sizes.

 

Here is the bit of code I think is causing the issue:

Line 29:

// Adds pretty filesizes
function pretty_filesize($file) {
    $size = filesize($file);
    if ($size < 1024) {
        $size = $size . " Bytes";
    } elseif (($size < 1048576) && ($size > 1023)) {
        $size = round($size / 1024, 1) . " KB";
    } elseif (($size < 1073741824) && ($size > 1048575)) {
        $size = round($size / 1048576, 1) . " MB";
    } else {
        $size = round($size / 1073741824, 1) . " GB";
    }
    return $size;
}

and line 184:

            $sizekey=filesize($dirArray[$index]);

And the referenced $dirArray[index]:

// Loops through the array of files
for ($index = 0; $index < $indexCount; $index++) {
 
    // Decides if hidden files should be displayed, based on query above.
    if (substr($dirArray[$index], 0, 1) != $hide || ($currdir != "." && $dirArray[$index] == "..")) {
 
        // Resets Variables
        $favicon = "";
        $class = "file";
 
        // Gets File Names
        $name = $dirArray[$index];
        $namehref = ($currdir == "." ? "" : $currdir . '/') . $dirArray[$index];
 
        // Gets Date Modified
        $modtime = date("M j Y g:i A", filemtime($dirArray[$index]));
        $timekey = date("YmdHis", filemtime($dirArray[$index]));
 

Not sure what is throwing the error...

 

Thanks in advance

Link to comment
Share on other sites

But as soon as I change directories I get:

Warning: filesize(): stat failed for blahblahblah on line blah

 

...

 

Not sure what is throwing the error...

blahblahblah is throwing the error on line blah. Have a look at that file and that line (and a couple of lines before). You could debug this by printing the $file value when $size becomes false (i.e. the call failed).

 

Most likely, the problem is the path. filesize requires the full path of the file to be checked. If a relative path (or just a filename) is provided, it will look in the current working directory.

 

It might also be a permission issue, but since only you saw the error message, we can't really say.

 

Note: When posting error messages on this site, post the entire message. You can X-out any sensitive information if you want. In my experience, PHP has very few (if any) cryptic error messages, if you read the message it tells you what, when, where and why.

Link to comment
Share on other sites

It would be the path they navigate to.

 

The code you showed does not provide enough information about where the filenames came from or even what is in the array. If you are using glob or readdir, you will need to prepend the same path you gave those functions to the filename you are checking.

 

Is your $namehref variable getting the correct value? If so, maybe you want to use the same thing you did there.

 

Also, your calls to filemtime should be having the same problem as the call to filesize.

Link to comment
Share on other sites

Well here is the whole code.

<!doctype html>
<html>
    <head>
        <meta charset="UTF-8">
        <link rel="shortcut icon" href="./.favicon.ico">
        <title>Houston Pilots</title>
 
        <link rel="stylesheet" href="./.style.css">
        <script src="./.sorttable.js"></script>
    </head>
 
    <body>
        <div id="container">
            <h1>Houston Pilots- Current Documents</h1>
 
            <table class="sortable">
                <thead>
                    <tr>
                        <th>Filename</th>
                        <th>Type</th>
                        <th>Filesize</th>
                        <th>Date Modified</th>
                    </tr>
                </thead>
                <tbody><?php
 
// Adds pretty filesizes

function pretty_filesize($file) {
    $file = ("./");
    $size = filesize($file);
    if ($size < 1024) {
        $size = $size . " Bytes";
    } elseif (($size < 1048576) && ($size > 1023)) {
        $size = round($size / 1024, 1) . " KB";
    } elseif (($size < 1073741824) && ($size > 1048575)) {
        $size = round($size / 1048576, 1) . " MB";
    } else {
        $size = round($size / 1073741824, 1) . " GB";
    }
    return $size;
}
 
$hide = ".";
 
if (!isset($_SERVER['QUERY_STRING']) || $_SERVER['QUERY_STRING'] == "" || $_SERVER['QUERY_STRING'] == "..") {
    $currdir = "z:";
} else {
    $currdir = $_SERVER['QUERY_STRING'];
}
 
// Opens directory
$myDirectory = opendir($currdir);

// Gets each entry
while ($entryName = readdir($myDirectory)) {
    $dirArray[] = $entryName;
}
 
// Closes directory
closedir($myDirectory);
 
// Counts elements in array
$indexCount = count($dirArray);
 
// Sorts files
sort($dirArray);
 
// Loops through the array of files
for ($index = 0; $index < $indexCount; $index++) {
 
    // Decides if hidden files should be displayed, based on query above.
    if (substr($dirArray[$index], 0, 1) != $hide || ($currdir != "." && $dirArray[$index] == "..")) {
 
        // Resets Variables
        $favicon = "";
        $class = "file";
 
        // Gets File Names
        $name = $dirArray[$index];
        $namehref = ($currdir == "." ? "" : $currdir . '/') . $dirArray[$index];
 
        // Gets Date Modified
        $modtime = date("M j Y g:i A", @filemtime($dirArray[$index]));
        $timekey = date("YmdHis", @filemtime($dirArray[$index]));
 
 
        // Separates directories, and performs operations on those directories
        if (is_dir($currdir . '/' . $dirArray[$index])) {
            $extn = "<Directory>";
            $size = "<Directory>";
            $sizekey = "0";
            $class = "dir";
 
            // Gets favicon.ico, and displays it, only if it exists.
            if (file_exists("$namehref/favicon.ico")) {
                $favicon = " style='background-image:url($namehref/favicon.ico);'";
                $extn = "<Website>";
            }
 
            // Cleans up . and .. directories
            if ($name == ".") {
                $name = ". (Current Directory)";
                $extn = "<System Dir>";
                $favicon = " style='background-image:url($namehref/.favicon.ico);'";
            }
            if ($name == "..") {
                $name = ".. (Parent Directory)";
                $extn = "<System Dir>";
            }
            if ($currdir == "." && $dirArray[$index] == "..")
                $namehref = "";
            elseif ($dirArray[$index] == "..") {
                $dirs = explode('/', $currdir);
                unset($dirs[count($dirs) - 1]);
                $prevdir = implode('/', $dirs);
                $namehref = '?' . $prevdir;
            }
            else
                $namehref = '?' . $namehref;
        }
 
        // File-only operations
        else {
            // Gets file extension
            $extn = pathinfo($dirArray[$index], PATHINFO_EXTENSION);
 
            // Prettifies file type
            switch ($extn) {
                case "png": $extn = "PNG Image";
                    break;
                case "jpg": $extn = "JPEG Image";
                    break;
                case "jpeg": $extn = "JPEG Image";
                    break;
                case "svg": $extn = "SVG Image";
                    break;
                case "gif": $extn = "GIF Image";
                    break;
                case "ico": $extn = "Windows Icon";
                    break;
                 case "txt": $extn = "Text File";
                    break;
                case "log": $extn = "Log File";
                    break;
                case "htm": $extn = "HTML File";
                    break;
                case "html": $extn = "HTML File";
                    break;
                case "xhtml": $extn = "HTML File";
                    break;
                case "shtml": $extn = "HTML File";
                    break;
                case "php": $extn = "PHP Script";
                    break;
                case "js": $extn = "Javascript File";
                    break;
                case "css": $extn = "Stylesheet";
                    break;
                 case "pdf": $extn = "PDF Document";
                    break;
                case "xls": $extn = "Spreadsheet";
                    break;
                case "xlsx": $extn = "Spreadsheet";
                    break;
                case "doc": $extn = "Microsoft Word Document";
                    break;
                case "docx": $extn = "Microsoft Word Document";
                    break;
                 case "zip": $extn = "ZIP Archive";
                    break;
                case "htaccess": $extn = "Apache Config File";
                    break;
                case "exe": $extn = "Windows Executable";
                    break;
 
                default: if ($extn != "") {
                        $extn = strtoupper($extn) . " File";
                    } else {
                        $extn = "Unknown";
                    } break;
            }
 
            // Gets and cleans up file size
            $size = pretty_filesize($dirArray[$index]);
            $sizekey = filesize($dirArray[$index]);
        }
 
        // Output
        echo("
                <tr class='$class'>
                        <td><a href='$namehref'>$name</a></td>
                        <td><a href='$namehref'>$extn</a></td>
                        <td><a href='$namehref'>$size</a></td>
                        <td><a href='$namehref'>$modtime</a></td>
                </tr>");
    }
}
?>
                </tbody>
            </table>
        </div>
    </body>
</html>

See here is what we have going on (follow me on this one)

 

1) We have cloud storage. We also have could computing. 2 different services, same provider.

2) Cloud storage is utilized through Atmos GeoDrive (AT&T). So we had to install the access app on all of our clients, including the Cloud Server, to manage a file structure and files. (Drive Z:) on the cloud server

3) We also set up XAMPP on the cloud server to host a small site, to display the files on the cloud storage (Drive Z: through Atmos GeoDrive)

 

I can see the file structure when I go to the site running the above code. But when I click on any directories, I see the files, but no file sizes and I cannot retrieve or view any of the files, because it is point the files to file:///z: etc.

 

If I change the directory in the code to just "." instead of "z:" (Line 46,47) then I get the directory of the webroot, and I can click on files and retrieve them or view them.

 

I am soooo confused right now, it's not even funny.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.