www.webdeveloper.com
Results 1 to 15 of 15

Thread: Getting any filesize without FTP

  1. #1
    Join Date
    Oct 2005
    Posts
    102

    Getting any filesize without FTP

    Is this possible, and how? I want to get the file size of a file, but I may not have the ftp username/password. I found this little code:
    http://forums.devshed.com/php-develo...rl-102506.html
    But, I don't believe it works.

    So, is it possible, and what PHP fuction would I use? (I'll then look it up at php.net)

    Thanks.

  2. #2
    Join Date
    Oct 2005
    Posts
    102
    Does anyone know if this is possible?

    Thanks.

  3. #3
    Join Date
    Dec 2005
    Location
    India
    Posts
    490
    I'm not familiar with Curl..
    You can use "file" which returns the file ocntents in array format.(each line)
    After imploding it, you can get the file size.
    fileStr = implode("",file($url));
    strlen(fileStr) should give appropriate results ..
    give it a try..

  4. #4
    Join Date
    Dec 2005
    Location
    India
    Posts
    490
    ohh..strlen will give the length of the String.. not the size of the file.. sorry for that
    will try to think of some alternative

  5. #5
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,408
    If the file is on the local filesystem, just use the filesize() function. If a remote file, you could try this function you'll find in the users' notes on that page (assuming your PHP installation has the cURL functions enabled):
    PHP Code:
    function remote_filesize($url$user ""$pw "") {
       
    ob_start();
       
    $ch curl_init($url);
       
    curl_setopt($chCURLOPT_HEADER1);
       
    curl_setopt($chCURLOPT_NOBODY1);
       if(!empty(
    $user) && !empty($pw)) {
           
    $headers = array('Authorization: Basic ' base64_encode("$user:$pw"));
           
    curl_setopt($chCURLOPT_HTTPHEADER$headers);
       }
       
    $ok curl_exec($ch);
       
    curl_close($ch);
       
    $head ob_get_contents();
       
    ob_end_clean();
       
    $regex '/Content-Length:\s([0-9].+?)\s/';
       
    $count preg_match($regex$head$matches);

       return isset(
    $matches[1]) ? $matches[1] : "unknown";

    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  6. #6
    Join Date
    Jan 2005
    Location
    Alicante (Spain)
    Posts
    7,742
    Using a curl or a custom function for this is padding. Content-Length is only sent in response to GET/POST (not HEAD) requests. This means a file download will be iniciated and due to PHP being a single thread language it is not possible to abort that file download midstream. This means the only way to ascertain the size of a remote file is to download it in its entirity. Whether once it is local you use Content-Length or strlen() to determine its size is irrelevant.

    PHP Code:
    $filesize strlen(file_get_contents($url)); 
    Last edited by bokeh; 09-20-2006 at 10:38 AM.

  7. #7
    Join Date
    Oct 2005
    Posts
    102
    Quote Originally Posted by NogDog
    If the file is on the local filesystem, just use the filesize() function. If a remote file, you could try this function you'll find in the users' notes on that page (assuming your PHP installation has the cURL functions enabled):
    PHP Code:
    function remote_filesize($url$user ""$pw "") {
       
    ob_start();
       
    $ch curl_init($url);
       
    curl_setopt($chCURLOPT_HEADER1);
       
    curl_setopt($chCURLOPT_NOBODY1);
       if(!empty(
    $user) && !empty($pw)) {
           
    $headers = array('Authorization: Basic ' base64_encode("$user:$pw"));
           
    curl_setopt($chCURLOPT_HTTPHEADER$headers);
       }
       
    $ok curl_exec($ch);
       
    curl_close($ch);
       
    $head ob_get_contents();
       
    ob_end_clean();
       
    $regex '/Content-Length:\s([0-9].+?)\s/';
       
    $count preg_match($regex$head$matches);

       return isset(
    $matches[1]) ? $matches[1] : "unknown";

    Ok, thanks. I'll give this a try.

    But, how will I know if curl is installed? I have a host, and the php on there is 4.4.2, not the most up to date. However, they are trying to work on a split of PHP 5 and PHP 4.

  8. #8
    Join Date
    Dec 2005
    Location
    India
    Posts
    490
    Quote Originally Posted by Bokeh
    $filesize = strlen(file_get_contents($url));
    If I'm not wrong, the term "size of file" should give the number of bytes occupied by the file. strlen will return the length of the file, but not the size of the file.
    I have a very very crude way for calculating this..
    read the file using file_get_contents or by the method i described above.
    Save the string as a tmp local file. then use filesize to get the size of the file.

  9. #9
    Join Date
    Jan 2005
    Location
    Alicante (Spain)
    Posts
    7,742
    Quote Originally Posted by sridhar_423
    If I'm not wrong, the term "size of file" should give the number of bytes occupied by the file. strlen will return the length of the file, but not the size of the file.
    The number of bytes and the file length is the same.

  10. #10
    Join Date
    Dec 2005
    Location
    India
    Posts
    490
    gr8.. I never knew tat.. thnx for that Bokeh.
    but new doubts have cropped up in my mind...

    I have chked for normal text. It worked perfectly fine. But failed for files containing characters of a different charset. (I chkd for a local Asian language).

    pls find below the details of the file which shows the file size on disk.
    -rw-r--r-- 1 pin2 pin 6026 Sep 21 13:01 two.asd

    But when i used the following code, i got just 39 as the o/p.
    $k = implode("",file("two.php"));
    echo strlen($k);

    where am I missing?
    ps: cudn't chk with file_get_contents (4.1.3)

    thanks
    Sridhar

  11. #11
    Join Date
    Jan 2005
    Location
    Alicante (Spain)
    Posts
    7,742

  12. #12
    Join Date
    Oct 2005
    Location
    Gold Coast, Australia
    Posts
    2,115
    Try this function then. It makes use of the file() function which should work on your platform.
    PHP Code:
    function remote_filesize($url$filesize_format true) {
        if(!
    $tab file($url)) {
            return 
    false;
        }
        
    $tab implode(''$tab);
        return (
    $filesize_format) ? filesize_format(strlen($tab)) : strlen($tab);
    }

    function 
    filesize_format($size$format ''$precision 2) {
        
    $sizes = array('TB' => 1099511627776'GB' => 1073741824'MB' => 1048576'KB' => 1024'B' => 1);
        foreach(
    $sizes as $unit => $bytes) {
            if(
    $size $bytes || $unit == strtoupper($format)) {
                return 
    number_format($size $bytes,$precision) . ' ' $unit;
            }
        }
    }

    echo 
    remote_filesize('http://www.google.com'); // 2.54 KB
    echo remote_filesize('http://www.google.com'false); // 2599 

  13. #13
    Join Date
    Dec 2005
    Location
    India
    Posts
    490
    Quote Originally Posted by Bokeh
    Why don't you get an up to date version? After all it is free.
    haha.. Bokeh became angry
    I haven't installed anything on my machine.. there is some "test_area" sort of space in our server and I do all my experiments over there.. I hav to install from end-to-end.. which I have been postponing from a long time.. newayz.. i'll take ur advice seriously ..I'll upgrade my version..

  14. #14
    Join Date
    Oct 2005
    Posts
    102
    Ok, I can't tell, but does that move the file to my server?

  15. #15
    Join Date
    Jan 2005
    Location
    Alicante (Spain)
    Posts
    7,742
    Interestingly that returns 1,024.00 GB for "1099511627776" and 1 TB for "1099511627776.1"
    Quote Originally Posted by pcthug
    PHP Code:
    array('TB' => 1099511627776'GB' => 1073741824'MB' => 1048576'KB' => 1024'B' => 1); 
    Anyway the concept of downloading a terrabyte of data to find the file size seems scary to me. A terrabyte equates to 58 hours download over a T3 or 70 days over a T1.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles