I am trying to get the fileSize of a file using this answer
this is my php code
<?php
echo filesize('LICENSE.txt');
echo filesize('http://cdn.bloody-disgusting.com/wp-content/uploads/2013/10/Victoria-Justice-HD-Wallpapers.jpg');
?>
Above code only works for local files but when I try to get filesize of CDN URL
, I get error
Warning: filesize() [function.filesize]: stat failed for http://cdn.bloody-disgusting.com/wp-content/uploads/2013/10/Victoria-Justice-HD-Wallpapers.jpg in test_db_operation() (line 80 of myphp.php).
Based on the filesize
php manual here
Description ¶
int filesize ( string $filename )
Gets the size for the given file.
Parameters ¶
**filename
Path to the file.
**Return Values ¶
Returns the size of the file in bytes, or FALSE (and generates an error of level E_WARNING) in case of an error.
Does filesize
not work with URL
or I am doing something wrong here. Please help !!! Thanks
Use this code to find the file size...
<?php
$remoteFile = 'http://cdn.bloody-disgusting.com/wp-content/uploads/2013/10/Victoria-Justice-HD-Wallpapers.jpg';
$ch = curl_init($remoteFile);
curl_setopt($ch, CURLOPT_NOBODY, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); //not necessary unless the file redirects (like the PHP example we're using here)
$data = curl_exec($ch);
curl_close($ch);
if ($data === false) {
echo 'cURL failed';
exit;
}
$contentLength = 'unknown';
$status = 'unknown';
if (preg_match('/^HTTP\/1\.[01] (\d\d\d)/', $data, $matches)) {
$status = (int)$matches[1];
}
if (preg_match('/Content-Length: (\d+)/', $data, $matches)) {
$contentLength = (int)$matches[1];
}
echo 'HTTP Status: ' . $status . "\n";
echo 'Content-Length: ' . $contentLength;
?>
Result:
HTTP Status: 302
Content-Length: 8808759
SOURCE : http://php.net/manual/en/function.filesize.php#92462