Page 1 of 2 12 LastLast
Results 1 to 10 of 14

Thread: help with file size with php

  1. #1
    Join Date
    Jan 2011
    Location
    Southeastern CT
    Posts
    612
    Thanks
    46
    Thanked 32 Times in 32 Posts

    Default help with file size with php

    I have a working directoery lister that I was able to adapt some code to but I am getting frustrated in a atempt to get the file sizes to be more precise.

    Php is not my strongest thing-lol

    this is used in a file to get file sizes:


    PHP Code:
    $size=number_format(filesize($dirArray[$index])); 


    I found this code but the array in the first example is messing me up.


    PHP Code:
    [COLOR="#FF0000"]function getFilesize($file,$digits 2) {
           if (
    is_file($file)) {
                   
    $filePath $file;
                   if (!
    realpath($filePath)) {
                           
    $filePath $_SERVER["DOCUMENT_ROOT"].$filePath;
           }[/
    COLOR]
               
    $fileSize filesize($filePath);
                   
    $sizes = array("TB","GB","MB","KB","B");
                   
    $total count($sizes);
                   while (
    $total-- && $fileSize 1024) {
                           
    $fileSize /= 1024;
                           }
                   return 
    round($fileSize$digits)." ".$sizes[$total];
           }
           return 
    false;

    I tried first to remove the red highlighted part that did not work.

    I tried this too and it did not work

    PHP Code:
    $fileSize filesize($filePath);
                   
    $sizes = array("TB","GB","MB","KB","B");
                   
    $total count($sizes);
                   while (
    $total-- && $fileSize 1024) {
                           
    $fileSize /= 1024;
                           }
                   return 
    round($fileSize$digits,$dirArray[$index])." ".$sizes[$total];
           }
           return 
    false;

    Any hint where I should be going with this?

    the original script shows only byte size.which can be large for some stuff-lol

    If needed,I can put up a link to a text file of the working script.

    thanks in advance
    Last edited by ajfmrf; 02-21-2014 at 02:13 PM. Reason: to post resolved
    Thanks,

    Bud

  2. #2
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    Quote Originally Posted by ajfmrf View Post
    I have a working directoery lister that I was able to adapt some code to but I am getting frustrated in a atempt to get the file sizes to be more precise.
    What do you mean by "more precise"? filesize is as accurate as you'll get. If you're getting incorrect results, it may be a caching issue: try calling clearstatcache first. Another possible issue would be trying to check files over 2GB on a 32-bit system, but this is unlikely.

    Quote Originally Posted by ajfmrf View Post
    PHP Code:
    function getFilesize($file,$digits 2) {
           if (
    is_file($file)) {
                   
    $filePath $file;
                   if (!
    realpath($filePath)) {
                           
    $filePath $_SERVER["DOCUMENT_ROOT"].$filePath;
           }
               
    $fileSize filesize($filePath);
                   
    $sizes = array("TB","GB","MB","KB","B");
                   
    $total count($sizes);
                   while (
    $total-- && $fileSize 1024) {
                           
    $fileSize /= 1024;
                           }
                   return 
    round($fileSize$digits)." ".$sizes[$total];
           }
           return 
    false;

    I tried first to remove the red highlighted part that did not work.
    What do you mean by "did not work"? What are you trying to accomplish? This function works as expected for me: it returns the filesize in (as appropriate) Bytes, KB, MB, GB, TB.

    Quote Originally Posted by ajfmrf View Post
    I tried this too and it did not work

    PHP Code:
    $fileSize filesize($filePath); 
                   
    $sizes = array("TB","GB","MB","KB","B"); 
                   
    $total count($sizes); 
                   while (
    $total-- && $fileSize 1024) { 
                           
    $fileSize /= 1024
                           } 
                   return 
    round($fileSize$digits,$dirArray[$index])." ".$sizes[$total]; 
           } 
           return 
    false

    That's not even syntactically correct - it would cause a fatal error. Do you use a code editor? It would be able to catch errors like this. Even something with simple syntax highlighting would go a long way towards helping you catch syntax problems.

  3. #3
    Join Date
    Jan 2011
    Location
    Southeastern CT
    Posts
    612
    Thanks
    46
    Thanked 32 Times in 32 Posts

    Default

    Okay Adrian here is the whole file I have

    PHP Code:
    <!doctype html>
    <html>

    <head>
        <meta charset="UTF-8">
        <title>Directory Contents</title>
        <link rel="stylesheet" href=".style2.css">
        <script src=".sorttable.js"></script>
    </head>

    <body>

        <div id="container">
        
            <h1>Directory Contents</h1>
            
            <table class="sortable">
                <thead>
                    <tr>
                        <th>Filename</th>
                        <th>Type</th>
                        <th>Size <small>(bytes)</small></th>
                        <th>Date Modified</th>
                    </tr>
                </thead>
                <tbody>
                <?php
                    
    // Opens directory
                    
    $myDirectory=opendir(".");
                    
                    
    // Gets each entry
                    
    while($entryName=readdir($myDirectory)) {
                        
    $dirArray[]=$entryName;
                    }
                    
                    
    // Finds extensions of files
                    
    function findexts ($filename) {
                        
    $filename=strtolower($filename);
                        
    $exts=split("[/\\.]"$filename);
                        
    $n=count($exts)-1;
                        
    $exts=$exts[$n];
                        return 
    $exts;
                    }
                    
                    
    // 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++) {
                    
                        
    // Allows ./?hidden to show hidden files
                        
    if($_SERVER['QUERY_STRING']=="hidden")
                        {
    $hide="";
                        
    $ahref="./";
                        
    $atext="Hide";}
                        else
                        {
    $hide=".";
                        
    $ahref="./?hidden";
                        
    $atext="Show";}
                        if(
    substr("$dirArray[$index]"01) != $hide) {
                        
                        
    // Gets File Names
                        
    $name=$dirArray[$index];
                        
    $namehref=$dirArray[$index];
                        
                        
    // Gets Extensions 
                        
    $extn=findexts($dirArray[$index]); 
                        
                        
    // Gets file size 
                        
    [COLOR="#FFFF00"]$size=number_format(filesize($dirArray[$index]));[/COLOR]
                        
                        
    // Gets Date Modified Data
                        
    $modtime=date("M j Y g:i A"filemtime($dirArray[$index]));
                        
    $timekey=date("YmdHis"filemtime($dirArray[$index]));
                        
                        
    // Prettifies File Types, add more to suit your needs.
                        
    switch ($extn){
                            case 
    "png"$extn="PNG Image"; break;
                            case 
    "jpg"$extn="JPEG Image"; break;
                            case 
    "svg"$extn="SVG Image"; break;
                            case 
    "images/gifgif"$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 
    "php"$extn="PHP Script"; break;
                            case 
    "js"$extn="Javascript"; break;
                            case 
    "css"$extn="Stylesheet"; break;
                            case 
    "pdf"$extn="PDF Document"; break;
                            
                            case 
    "zip"$extn="ZIP Archive"; break;
                            case 
    "bak"$extn="Backup File"; break;
                            
                            default: 
    $extn=strtoupper($extn)." File"; break;
                        }
                        
                        
    // Separates directories
                        
    if(is_dir($dirArray[$index])) {
                            
    $extn="&lt;Directory&gt;"
                            
    $size="&lt;Directory&gt;"
                            
    $class="dir";
                        } else {
                            
    $class="file";
                        }
                        
                        
    // Cleans up . and .. directories 
                        
    if($name=="."){$name=". (Current Directory)"$extn="&lt;System Dir&gt;";}
                        if(
    $name==".."){$name=".. (Parent Directory)"$extn="&lt;System Dir&gt;";}
                        
                        
    // Print 'em
                        
    print("
                        <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 sorttable_customkey='
    $timekey'><a href='./$namehref'>$modtime</a></td>
                        </tr>"
    );
                        }
                    }
                
    ?>
                </tbody>
            </table>
        
            <h2><?php print("<a href='$ahref'>$atext hidden files</a>"); ?></h2>
            
        </div>
        
    </body>

    </html>
    this results in the file size being posted the long way

    examples are 65,049bytes,496,928bytes
    I am trying to get that to put it into rounded kb,mb,gb or tb if it is of one type size or the other.

    I am probally not wording this correctly.

    I tried to change the yellow part to make this change and is is not working.I get errors and have tried many ways-too many to list here -to work this out.

    I tried to get the "file size" part to be yellow but it is not working???
    Last edited by ajfmrf; 02-19-2014 at 06:05 PM. Reason: color not working?
    Thanks,

    Bud

  4. #4
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    Quote Originally Posted by ajfmrf View Post
    this results in the file size being posted the long way
    examples are 65,049bytes,496,928bytes
    I am trying to get that to put it into rounded kb,mb,gb or tb if it is of one type size or the other.
    The getFilesize function from your original post does this—you're not using it here. Have you tried it? Something like
    PHP Code:
    <?php
    //  . . .

    $size getFilesize$dirArray[$index] );

    //  . . .
    Quote Originally Posted by ajfmrf View Post
    I get errors and have tried many ways-too many to list here -to work this out.
    Well, just start with the one that seemed "most" successful, and the first (or first few) errors. It's always helpful.

    Quote Originally Posted by ajfmrf View Post
    I tried to get the "file size" part to be yellow but it is not working???
    If you're talking about the BBCode tags, they don't work inside [PHP] or [HTML] tags (though they do work inside plain ol' [CODE] tags).

  5. #5
    Join Date
    Jan 2011
    Location
    Southeastern CT
    Posts
    612
    Thanks
    46
    Thanked 32 Times in 32 Posts

    Default

    Hi Adrian,that original part will give the file size to each file.

    However I was trying to get it to work differently.

    As it is originally it will give size s bytes only,even for those that are fairly big.

    like:
    60,593bytes or 396,659bytes

    I am trying to have it round to kb,mb if the size warrants it.

    I know it works fine giving file size I am just trying to get it to go to the next level so to speak.

    Am I confusing things-upgrade this to use kb,mb instead of just bytes ....
    Thanks,

    Bud

  6. #6
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    Quote Originally Posted by ajfmrf View Post
    Hi Adrian,that original part will give the file size to each file.

    However I was trying to get it to work differently.

    As it is originally it will give size s bytes only,even for those that are fairly big.

    like:
    60,593bytes or 396,659bytes

    I am trying to have it round to kb,mb if the size warrants it.

    I know it works fine giving file size I am just trying to get it to go to the next level so to speak.

    Am I confusing things-upgrade this to use kb,mb instead of just bytes ....
    Are we both talking about this function?
    PHP Code:
    function getFilesize($file,$digits 2) {
        if (
    is_file($file)) {
            
    $filePath $file;
            if (!
    realpath($filePath)) {
                
    $filePath $_SERVER["DOCUMENT_ROOT"].$filePath;
            }
            
    $fileSize filesize($filePath);
            
    $sizes = array("TB","GB","MB","KB","B");
            
    $total count($sizes);
            while (
    $total-- && $fileSize 1024) {
                
    $fileSize /= 1024;
            }
            return 
    round($fileSize$digits)." ".$sizes[$total];
        }
        return 
    false;

    Now, there are some things about it that I don't like, but when I test this function, it does return the file size in KB/MB/etc., whichever is most natural to the file size. Does it not do so for you? Can you provide an example where it fails to do so?

  7. #7
    Join Date
    Jan 2011
    Location
    Southeastern CT
    Posts
    612
    Thanks
    46
    Thanked 32 Times in 32 Posts

    Default

    Adrian.my problem is ,I don't know how to rewrite the function .

    Yes,we are talking about the same "new" function.

    I take the original one and tried to redo it many different ways and so far nothing has given me a working version.

    Each on returns errors.(I have tried many ways -too many to post -they don,t work)

    the ($dirArray[$index] I think is what has me confused ????
    Thanks,

    Bud

  8. #8
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    Quote Originally Posted by ajfmrf View Post
    Adrian.my problem is ,I don't know how to rewrite the function .

    Yes,we are talking about the same "new" function.

    I take the original one and tried to redo it many different ways and so far nothing has given me a working version.
    Alright- what I'm saying is, the getFilesize function (as written above, with no changes) works exactly as you describe when I test it.

    Quote Originally Posted by ajfmrf View Post
    Each on returns errors.(I have tried many ways -too many to post -they don,t work)
    You need to share the errors you are getting. I cannot help you by guessing. If there are "too many to post," start with the first few.

    I don't mean to be blunt, but there is no point in asking for help at all if you don't provide specific information about what is going wrong.

    Quote Originally Posted by ajfmrf View Post
    the ($dirArray[$index] I think is what has me confused ????
    What is confusing you about it? Have you checked (e.g., using var_dump) to make sure the variable and index contain the value you expect?

  9. #9
    Join Date
    Jan 2011
    Location
    Southeastern CT
    Posts
    612
    Thanks
    46
    Thanked 32 Times in 32 Posts

    Default

    okay-forget it.
    Thanks,

    Bud

  10. #10
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    Quote Originally Posted by ajfmrf View Post
    okay-forget it.
    ??

    I'm not trying to discourage you, I'm just asking you to tell us what the specific problem is. If you do so, I (or someone) can probably help you find a solution.

Similar Threads

  1. Looking for auto file size lister to go in html
    By SpaceNeedler in forum Looking for such a script or service
    Replies: 2
    Last Post: 11-29-2010, 06:11 PM
  2. Size of file on domain
    By bluewalrus in forum PHP
    Replies: 3
    Last Post: 10-24-2009, 06:45 PM
  3. Replies: 11
    Last Post: 08-09-2008, 08:18 AM
  4. File size problem - Directory size problem
    By davepatmanning@yahoo.com in forum The lounge
    Replies: 1
    Last Post: 05-09-2007, 07:17 PM
  5. Jim's DHTML Meny V5.7 File Size Problem
    By demtro in forum Dynamic Drive scripts help
    Replies: 3
    Last Post: 09-27-2005, 03:16 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •