Announcement

Collapse
No announcement yet.

Remote File Size

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Remote File Size

    Is there any way in PHP to determine the file size of a remote file?

  • #2
    this kinda works

    Jez:

    This script works somewhat, since you can not use the filesize() function on remote files.

    This script works best with just regular html and ASCII files, it does not give an accurate reading for binary files, like .zip files and such.

    PHP Code:
    <?php

    $contents 
    file("http://www.cnunited.com/index.php");

    for(
    $i=0$i<count($contents); $i++)
        
    $string .= $contents[$i];

    print 
    strlen($string);

    ?>
    Hope this helps.

    Comment


    • #3
      Re: this kinda works

      Originally posted by krs-one
      PHP Code:
      <?php

      $contents 
      file("http://www.cnunited.com/index.php");

      for(
      $i=0$i<count($contents); $i++)
          
      $string .= $contents[$i];

      print 
      strlen($string);

      ?>
      PHP Code:
      $string = @implode(''file("http://www.cnunited.com/index.php"));
      print 
      strlen($string); 
      even shorter

      Comment


      • #4
        Why stop there?
        PHP Code:
        print strlen(@implode(''file("http://www.cnunited.com/index.php"))); 
        or (3 characters shorter)
        PHP Code:
        print strlen(@join(''file("http://www.cnunited.com/index.php"))); 
        Muahahaha! Not even Santa Claus can save you now!

        Comment


        • #5
          But you have to download all the file. Heres what I finally came up with:

          PHP Code:
          function getRemoteSize($rURL) {
          $rHost ereg_replace("http://([^\/]+)/(.*)""\\1"$rURL);
          $rPath ereg_replace("http://([^\/]+)/(.*)""/\\2"$rURL);
          $fp fsockopen($rHost80, &$errno, &$errstr10);
          if (!
          $fp) {
            echo 
          "ERROR: $errno$errstr";
          } else {
            
          fputs($fp"HEAD $rPath HTTP/1.0\r\nHost: $rHost\r\nConnection: close\r\n\r\n");
            while (!
          feof($fp)) {
              
          $response .= fgets($fp128);
            }
          fclose($fp);
          }
          return 
          ereg_replace("(.*)(Content-Length: )([0-9]*)(.*)","\\3",$response);
          }

          echo 
          "File Size: ".getRemoteSize("http://www.php.net/"); 

          Comment


          • #6
            ........and how is that any different?

            BTW... both are inaccurate. Your getting the size of the output of the requested file. Not the actual size of the file. So if you try to get the size of a .php file, you'll get the size of what it print()s not the size of the script that print()ed it. Make sense?

            PHP Code:
            // 25 bytes of php
            // 11 bytes of printed content
            <? echo "Hello World"?>

            Comment


            • #7
              ALBERT HAS SPOKEN!
              *roaring thunder and lightning*

              In that case I would think it's impossible to get remote file size. Thats only if you are getting some sort of script or something. HTML, text, images, everything else should work fine.
              Last edited by Vergil; Sun 24 Jun '01, 8:42pm.

              Comment


              • #8
                haha!
                I hope I didn't sound snotty or stuck up. I was trying to sound thoughtfull. I like to question things. I think you learn more that way.

                That's true. I overlooked that point.

                Comment


                • #9
                  Originally posted by Max Albert
                  ........and how is that any different?
                  Umm...its VERY different. For a start it doesn't download all the file, only the header files directly from the server.

                  I wanted to get the file size of very large files, thats why I prefer this method.

                  Comment

                  widgetinstance 262 (Related Topics) skipped due to lack of content & hide_module_if_empty option.
                  Working...
                  X