Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How does a mirror check work without fopen?

Status
Not open for further replies.

jimoblak

Instructor
Oct 23, 2001
3,620
US
I have an image file that is mirrored on 3 separate servers. I hope to script a page that will display this image to the client regardless of which server the image resides. The script will check to see if server #1 is able to deliver the file. If not, the script will check server #2. The image file will be sent to the client directly from the mirrors. I do not want or need the image file to use up the bandwidth of the primary server that runs the script.

The problem is that all that I can imagine involves fopen. Is there a way to check the size and existence of a remote file without eating bandwidth on the primary server running the PHP script?

- - picklefish - -
Why is everyone in this forum responding to me as picklefish?
 
bandwith will be eaten if you wanna access any file on a secondary server. the only way out is to make sure that a file always exsits in the seconday server...

Known is handfull, Unknown is worldfull
 
Hi,
yiou can use the HTTP HEAD method request,
will transfer to you just a feew lines.

I've wrote a small function, you can use it

Code:
<?php

// check a file size  (HTTP URL)
// using HEAD request method
// return == -1  , means something is going wrong;
function get_url_file_size($url)
{
        $default_p = 80 ; // default http port
        $url_a = parse_url($url);
        if (empty($url_a['port']))
        {
                $url_a['port'] = $default_p;
        };
        if (!$sd = fsockopen($url_a['host'], $url_a['port']))
        {
                return -1 ; // can not connect
        };

        $out = "HEAD {$url_a['path']} HTTP/1.0\n".
                "Connection: Close\n\n\n";
        fwrite($sd, $out);
        while ($line= fgets($sd, 1024))
        {
                if (preg_match("/^Content-Length:\s+(.+)/i", $line, $matches))
                {
                        fclose($sd);
                        return $matches[1];
                };
        };
        fclose($sd);
        return -1;

}
echo get_url_file_size('[URL unfurl="true"]http://www.romtelecom.ro/templates/default/ro/page/img/logo.gif');[/URL]
?>

___
____
 
>>fsockopen($url_a['host'], $url_a['port']

that will take the primary server's bandwidth, if that is o.k why not use plain fopen()??? how does the performance change when fopen is used???

Known is handfull, Unknown is worldfull
 
if I'm not wrong, he wants also to check the file size.
If this is a big one, will save a lot of bandwith.

PM

PS: probable I have post in the same time with you, vbkris

___
____
 
then wont a plain <img src="FilePath"> do???

Known is handfull, Unknown is worldfull
 
Yes, the file size information is helpful to make sure that the image has not been altered or that the server is not replacing it with a 404 error document or replacement image.

PHP 5 seems to allow file_exists() on remote URLs but this script must be portable to older PHP-enabled servers.

I will tolerate fopen. The code will be posted as soon as it is cleaned up. When my mind is not so fluffy, I will try to incorporate predamarcel's offering.

- - picklefish - -
Why is everyone in this forum responding to me as picklefish?
 
If it helps anyone else, here is what I settled with.

What this does:
The following script takes an array of possible images (allegedly the same but on different servers) and randomly passes one to the client. If one server is offline, another server is used. If no servers are available, a message appears: 'no image is available'.

What this could do (with more tweaking):
Errors could happen if the image servers rename or replace an image with the same filename. Integration of predamarcel's script may help here to compare byte sizes to the expected image file.

Code:
<?php

$imagelist = array("[URL unfurl="true"]http://www.google.com/images/logo.gif","http://www.google.com/intl/en/logos/ray.gif","http://www.google.com/intl/en/logos/mothers_day04.gif");[/URL]
$totalitems = count($imagelist);
srand(time());
shuffle($imagelist);
$nofile=0;

   for ($x=0; $x<=($totalitems-1); $x++) {
   $imagefile=@fopen($imagelist[$x], "r");
      if ($imagefile)
      {
      $displayimage="$imagelist[$x]";
      $x=$totalitems;
      fclose($imagefile);
      }
   }

   if ($displayimage) {
   echo "<img src=\"$displayimage\">";
   } else {
   echo "no image is available";
}

?>

- - picklefish - -
Why is everyone in this forum responding to me as picklefish?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top