Saturday, June 14, 2008

PHP get remote file size function

PHP get remote file size function

When using PHP to retrieve a remote file system you can't just use filesize to get a remote file on a server.

you should also be prepared to know that some servers don't allow you to get the filesize each server has the ability to fake a filesize too.

but most of the times this will get the right filesize for a remote file.

This is my favorite way of reading a remote file because it is very simple. Just call this function and specify a url as the parameter. But make sure you remember to check the return value first to determine if it return an error before processing the result
<?php

$content 
file_get_contents('http://www.google.com/');
if (
$content !== false) {
   
// do something with the content } else {
   
// an error happened } ?>

Unlike the two methods above using CURL cannot be said as straigthforward. Although this library is very useful to connect and communicate with may different protocols ( not just http ) it requires more effort to learn. And another problem is that not all web host have this library in their php installation. So we better make sure to check if the library is installed before trying to use it.

Here is a basic example on fetching a remote file

<?php // make sure curl is installed if (function_exists('curl_init')) {
   
// initialize a new curl resource
   
$ch curl_init();

   
// set the url to fetch
   
curl_setopt($chCURLOPT_URL'http://www.google.com');

   
// don't give me the headers just the content
   
curl_setopt($chCURLOPT_HEADER0);

   
// return the value instead of printing the response to browser
   
curl_setopt($chCURLOPT_RETURNTRANSFER1);

   
// use a user agent to mimic a browser
   
curl_setopt($chCURLOPT_USERAGENT'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7.5) Gecko/20041107 Firefox/1.0');

   
$content curl_exec($ch);

   
// remember to always close the session and free all resources
   
curl_close($ch);
} else {
   
// curl library is not installed so we better use something else } ?>
In some cases using CURL is faster than using file_get_contents() or fopen(). This is because CURL handles compression protocols by default ( for example gzip ). Many sites, big and small, use gzip compression to compress their web pages in order to save bandwidth. This site, for example, also use gzip compression which cut the bandwidth used into half. So if you're the type who just can't wait CURL will fit you most.



If you use fopen() to read a remote file the process is as simple as reading from a local file. The only difference is that you will specify the URL instead of the file name. Take a look at the example below :

<?php // make sure the remote file is successfully opened before doing anything else if ($fp fopen('http://www.google.com/''r')) {
   
$content '';
   
// keep reading until there's nothing left
   
while ($line fread($fp1024)) {
      
$content .= $line;
   }

   
// do something with the content here
   // ...
} else {
   
// an error occured when trying to open the specified url } ?>
Now, the code above use fread() function in the while loop to read up to 1024 bytes of data in a single loop. That code can also be written like this :
<?php // make sure the remote file is successfully opened before doing anything else if ($fp fopen('http://www.google.com/''r')) {
   
$content '';
   
// keep reading until there's nothing left
   
while ($line fgets($fp1024)) {
      
$content .= $line;
   }

   
// do something with the content here
   // ...
} else {
   
// an error occured when trying to open the specified url } ?>
instead of fread() we use fgets() which reads one line of data up to 1024 bytes. The first code is much more preferable than the second though. Just imagine if the remote file's size is 50 kilobytes and consists of 300 lines. Using the first code will cause the loop to be executed about fifty times but using the second the loop will be executed three hundred times.

If you consider the cost to call a function plus the time required to make 300 requests compared to just 5 then clearly the first one is the winner.



Fyi here is the filesize for php

<?php
// outputs e.g.  somefile.txt: 1024 bytes
$filename 'somefile.txt';
echo 
$filename ': ' filesize($filename) . ' bytes';
?> 

No comments:

Post a Comment

Got a Suggestion please let us know