For Programmers: Free Programming Magazines  


Home > Archive > PHP Programming > February 2006 > obtaining the size of a file









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author obtaining the size of a file
Steve

2006-02-26, 3:55 am

I need to obtain the filesize of a file thats not on the same server. As
filesize() need a realpath and not a url how else can this be done?
Gordon Burditt

2006-02-26, 3:55 am

>I need to obtain the filesize of a file thats not on the same server. As
>filesize() need a realpath and not a url how else can this be done?


Fetch the file and look at the size of it?

How *can* you access this file? Some FTP servers will provide the
file size if you ask them nicely. HTTP doesn't (unless you get the
file too). And if it's dynamic content, it might be a different
size every time you fetch it.

Gordon L. Burditt

Steve

2006-02-26, 3:55 am


>
> Fetch the file and look at the size of it?
>
> How *can* you access this file? Some FTP servers will provide the
> file size if you ask them nicely. HTTP doesn't (unless you get the
> file too). And if it's dynamic content, it might be a different
> size every time you fetch it.


Hmm, could you make a HEAD request? That has a content size header,
although I don't know if you'd have to factor in the size of the header
itself and whether any encoding is used.

---
Steve

Janwillem Borleffs

2006-02-26, 7:55 am

Steve wrote:
> Hmm, could you make a HEAD request? That has a content size header,
> although I don't know if you'd have to factor in the size of the
> header itself and whether any encoding is used.
>


Sure, per ezample:

$host = 'www.google.com';
$path = '/images/logo.gif';
$bytes = 0;

if ($fp = fsockopen($host, 80)) {
fputs($fp, "HEAD $path HTTP/1.0\r\n");
fputs($fp, "Host: $host\r\n\r\n");
while (!feof($fp) && !$bytes) {
if (preg_match('/content\-length\s*:\s*(\d+)/i',
fgets($fp, 1024),
$m)) {
$bytes = $m[1];
}
}
fclose($fp);
}

print "size is $bytes bytes";


JW


Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2010 codecomments.com