note 32711 added to function.readfile

From: Date: Thu, 05 Jun 2003 01:14:00 +0000
Subject: note 32711 added to function.readfile
Groups: php.notes 
Request: Send a blank email to php-notes+get-49752@lists.php.net to get a copy of this message
This method does a remote copy WITHOUT the need for "fopen wrappers". If your host doesn't offer the ability to copy remote files with "readfile", you can use this one as a workaround. This is how you can read a file (in this case an image) from a remote server and serve it locally. This script keeps copies of the files in a subdirectory "files/" (make this one writable) to make sure that speed is optimal, to make sure that no problems are faced when the remote server is down and to make sure that traffic is as cost-effective as possible. If you want to do a live copy-and-serve (for whatever purposes), you only have to clean up this script. <? if (!file_exists("files/".$HTTP_GET_VARS[id].".jpg")){ $rhost="http://www.yourremotehost.com"; $fp1 = fopen($rhost."/images/image.php?id=".$HTTP_GET_VARS[id],"r"); $fp2 = fopen("files/".$HTTP_GET_VARS[id].".jpg","w"); while(!feof($fp1)) { $line = fgets($fp1, 1024); fputs($fp2,$line,strlen($line)); } fclose($fp1); fclose($fp2); } $fp=fopen("files/".$HTTP_GET_VARS[id].".jpg","r"); $contents = fread ($fp, filesize("files/".$HTTP_GET_VARS[id].".jpg")); fclose($fp); header("Content-Type: image/jpeg"); header("Content-Disposition: inline"); echo $contents; ?> Let's assume that you save this script as "image.php" in the html root of your site at yoursite.com, then you can call the script like this: http://www.yoursite.com/image.php?id=254 and it will show you the remote image file that is located here: http://www.yourremotehost.com/image/image.php?id=254 Should be working with big files as well, like downloads from another server. good luck ! Maarten Malaise *freak* ;-) ---- Manual Page -- http://www.php.net/manual/en/function.readfile.php Edit Note -- http://master.php.net/manage/user-notes.php?action=edit+32711 Delete Note -- http://master.php.net/manage/user-notes.php?action=delete+32711&report=yes Reject Note -- http://master.php.net/manage/user-notes.php?action=reject+32711&report=yes

« previous php.notes (#49752) next »