TunaFFish
TunaFFish

Reputation: 11302

PHP filesize for absolute path

I was surprised PHP's filesize() fails on absolute paths?? My files are on my own server, how can I get the filesize except from converting them to relative (a mess)

EDIT

example:

$filename = 'http://172.16.xx.x/app/albums/002140/tn/020.jpg';
echo $filename . ': ' . filesize($filename) . ' bytes';

Warning: filesize() [function.filesize]: stat failed for http://172.16.xx.x/app/albums/002140/tn/020.jpg in /Applications/XAMPP/xamppfiles/htdocs/app/admin/+tests/filesize.php on line 26

END EDIT

I found this example for remote files:

$filename = 'http://www.google.com/logos/2010/stevenson10-hp.jpg';
$headers = get_headers($filename, 1);
echo $headers['Content-Length']; // size in bytes

Does this work without downloading the files?

Upvotes: 3

Views: 14194

Answers (7)

Aayush
Aayush

Reputation: 18

To use Function filesize();

you should have absolute path not the urls

http://www.php.net//manual/en/function.filesize.php

Upvotes: 1

Pavnish Yadav
Pavnish Yadav

Reputation: 2778

You can use like this ... for get file size by URL

$ch = curl_init('http://172.16.xx.x/app/albums/002140/tn/020.jpg');
 curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
 curl_setopt($ch, CURLOPT_HEADER, TRUE);
 curl_setopt($ch, CURLOPT_NOBODY, TRUE);
 $data = curl_exec($ch);
 $size = curl_getinfo($ch, CURLINFO_CONTENT_LENGTH_DOWNLOAD);

 curl_close($ch);
 echo $size;

Upvotes: 2

Roopendra
Roopendra

Reputation: 7762

Yes It will be work fine .

$filename = 'http://172.16.xx.x/app/albums/002140/tn/020.jpg';

$headers  = get_headers($filename, 1);

$fsize    = $headers['Content-Length'];

Upvotes: 2

Halil Özgür
Halil Özgür

Reputation: 15945

http://172.16.xx.x/app/albums/002140/tn/020.jpg is not an absolute path, it is an URL. The absolute path for it would be something like /var/www/app/albums/002140/tn/020.jpg. You should use that absolute path in filesize().

filesize() supports only URL wrappers that support stat(). HTTP and HTTPS doesn't support that as mentioned in the manual page for HTTP and HTTPS wrappers.

Upvotes: 5

Sébastien VINCENT
Sébastien VINCENT

Reputation: 1106

get_headers() will send a GET to your server, this add load to your web server.

I don't get it, your filesize() fails on absolute path ? It should not. According to php.net : http://www.php.net/manual/en/wrappers.file.php

Edit: I'm not sure about the error PHP will give you if allow_url_fopen is set to 0, but check this line in your PHP.ini (and restart apache then) : http://www.php.net/manual/en/filesystem.configuration.php#ini.allow-url-fopen

If it's off, filesize() will not handle URL. Let me know if it was that.

Upvotes: 0

Oliver Charlesworth
Oliver Charlesworth

Reputation: 272497

As I suspected, you're trying to give an HTTP URL to filesize(), which will not work. filesize() works on local filesystem URLs, such as those listed at http://www.php.net/manual/en/wrappers.file.php.

Presumably, as you're trying to access files on your own server, you must have the filesystem URL, rather than just an HTTP URL?

Upvotes: 1

pop850
pop850

Reputation: 3157

I think you would have to use the filesize() function.

http://php.net/manual/en/function.filesize.php

echo filesize( $filename );

Upvotes: -3

Related Questions