Get Files From Remote File Size With PHP

Original source : Get Files From Remote File Size With PHP

To get the file size of local files in PHP, we can use the filesize () (http://php.net/manual/en/function.filesize.php), the function will produce the size of the requested file in bytes size. Unfortunately, these functions can not be used on remote files, or files which are beyond our servers. There are several ways you can use to read the file size of remote files using basic functions exist in PHP.

The First Method
The first way is to use curl (http://curl.haxx.se/). With this function we first read the whole remote file or in other words we download it in advance and stored in the tmp folder. Generally these functions have been found on engines PHP> 5.

[php]
$url    = 'http://www.google.com/intl/en_ALL/images/srpr/logo1w.png';

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$site = curl_exec($ch);
curl_close($ch);

if (preg_match('/Content-Length: (\d+)/', $site, $matches)) {
$contentLength = (int)$matches[1];
}

echo $contentLength; // size of Google logo in bytes
[/php]

Get File Size From Remote File With PHP

Get File Size From Remote File With PHP

The Second Method (The Best and Fastest!)
The second way is to use get_headers function (), which was introduced since PHP version 5, so if you're still using PHP version 4, then you can not perform this function. With this function, your server does not need to download the entire file, making it very fast and saves your servers resources.

[php]
Original source : Get Files From Remote File Size With PHP
[/php]

Hopefully this short article could help you and can solve problems in reading a remote file :)

Original source : Get Files From Remote File Size With PHP

About the Author:
Founder of Buzzknow

My other site

Remote Wireless Speakers Online Degree Higher Education

Author: Ferri Sutanto