SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    SitePoint Wizard
    Join Date
    Feb 2007
    Location
    Southern California
    Posts
    1,316
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    How to echo size of PDFs?

    I have a list of PDF files the visitor can download. I'd like to display the file size of each PDF. Is there a PHP script for determining a PDF's file size so I can echo it? Also, the file may be an HTML page. Can the file size script work with HTML and PDF formats?

    Thanks!

  2. #2
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  3. #3
    SitePoint Wizard
    Join Date
    Feb 2007
    Location
    Southern California
    Posts
    1,316
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by SilverBulletUK View Post
    This is how I used it (this is line 171):

    PHP Code:
    $size filesize('$Path$Filename'); 
    $Path is the URL leading up to the filename, and $Filename is the actual filename.

    It generated a warning, so I must be doing something wrong. That's why I came here. The warning is:

    Warning: filesize() [function.filesize]: stat failed for $Path$Filename in C:\wamp\www\resources\resources.inc.php on line 171

  4. #4
    SitePoint Wizard
    Join Date
    Feb 2007
    Location
    Southern California
    Posts
    1,316
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    The surrounding code:

    PHP Code:
    while ($sql mysql_fetch_array($sql2))
        { 
        
    $Docdesc =    $sql['Docdesc']; 
        
    $Path =        $sql['Path']; 
        
    $Filename =    $sql['Filename']; 

        
    $Docdesc =        htmlspecialchars($Docdesc);
        
    $Path =        htmlspecialchars($Path);
        
    $Filename =        htmlspecialchars($Filename);
        
    $size filesize('$Path$Filename');

        
    /* Define OPEN button. Clicking on button opens new window to display file. */
        
    $button "<a href='$Path$Filename' target='_blank'><img src='../imagestemplate/but_open.gif' alt='Open the file /></a>";

        echo 
    "<div class='descbox'>$size $button";
        echo 
    " <p 'style=text-align:left'> $Docdesc</p></div>";
        echo 
    "<div class='clear'></div>\n";
        } 
    Changing filesize('$Path$Filename'); to just filesize('$Filename'); generates the same warning.

  5. #5
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    $size filesize("$Path$Filename"); 
    *note the change to double quotes rather than single, this tells PHP to parse the variables contained within.

    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  6. #6
    SitePoint Wizard
    Join Date
    Feb 2007
    Location
    Southern California
    Posts
    1,316
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    $size = filesize("$Path$Filename"); still generates a warning:


    Warning: filesize() [function.filesize]: stat failed for http://aedownloads.com/pdf/manuals/25300_manual.pdf in C:\wamp\www\resources\resources.inc.php on line 175

    Thanks!

  7. #7
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Filesize() doesn't work with urls. You need to use a filesystem path. You can make you own which works with urls, but it's extremely inneficient.

  8. #8
    SitePoint Wizard
    Join Date
    Feb 2007
    Location
    Southern California
    Posts
    1,316
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by crmalibu View Post
    Filesize() doesn't work with urls. You need to use a filesystem path. You can make you own which works with urls, but it's extremely inneficient.
    So I should drop this project? Thanks for saving me some wasted time.

    Much appreciated,
    Steve

  9. #9
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Not neccesarily. Do you really need to know how many bytes the pdf is?

    You can determine the filesize a single time and save it in the database(where you store the url).

    You have two basic ways of determining the filesize.
    1) copy() the file to the filesystem, and then use filesize().
    2) Don't download the entire file, but just parse the http response headers. The Content-length: header will tell you how many bytes it is. The server is not required to send a content-length header, but the link you posted does, at least for now.

  10. #10
    SitePoint Wizard
    Join Date
    Feb 2007
    Location
    Southern California
    Posts
    1,316
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Some of the files are over 20MB, so users would like to know that in advance when they want to download something.

    I'll read up on parsing the http response headers for the content-length: header and see what I come up with.

    Thanks a ton!


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
  •