How can I cache a fetched image from a network and output it on the webserver?

I have a webcam set up on a computer on our University network.  This computer is visible to all other uni computers, including the webserver we use, but not to the outside world.  Currently the webcam is serving an image on port 80 of it's own computer.  I can link to this with an image tag in a file on the webserver and the pic will display when a campus computer requests it, but obviously not when the request is made from the outside world.  Therefore I need some PHP to fetch the image from the webcam server, cache it, and output it on the web page so that all can see the image.  Any ideas how to do this?
Who is Participating?
lozlozConnect With a Mentor Commented:

script by a friend of mine seems to suit your needs:


// Sam Overton 2002

$filename = "wcamimg.jpg";                  // file to cache in
$altfile = "images/unavail.gif";                  // alt image to use (if cam is down)
$update_time = 3;                        // fetch new image if last update was more than x seconds
$url = "";                  // url to fetch the image from
$maximgsize = 300*1024;                  // maximum amount of data it will read

// check if this is the first time we run the script (evar!)
$makenew = FALSE;
      $makenew = TRUE;

//get file modified time
$timestamp = filemtime($filename);
$thetime = time();
if($thetime - $timestamp > $update_time || $makenew)
      // we need to fetch a new copy
      $newfile = @fopen($url,"rb");
            // couldnt get a new copy!!!
            // use the alt image (webcam unavailable or soemthing)
            $newfile = fopen($altfile,"rb");

      // read in our data from the remote location
      $buffer = fread($newfile,$maximgsize);

      // store it locally
      $file = fopen($filename,"wb");

      // spit it out
      header("Content-type: image/jpeg");
      // local copy is recent enough so read it in
      $file = fopen($filename,"rb");
      $buffer = fread($file,filesize($filename));

      // spit, dont swallow :[
      header("Content-type: image/jpeg");

then you can just use <img src="webcam.php" border="0"> or something in any other page to get the image

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.