开发者

PHP filesize for absolute path

开发者 https://www.devze.com 2023-01-25 03:08 出处:网络
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)

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/XA开发者_如何学PythonMPP/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?


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.


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'];


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;


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?


To use Function filesize();

you should have absolute path not the urls

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


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.


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

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

echo filesize( $filename );
0

精彩评论

暂无评论...
验证码 换一张
取 消