note 32711 deleted from function.readfile by tomsommer
| From: | tomsommer@php.net | Date: | Mon, 09 Aug 2004 06:48:15 +0000 |
| Subject: | note 32711 deleted from function.readfile by tomsommer | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-74293@lists.php.net to get a copy of this message | ||
Note Submitter: maarten.malaise@pandora.be
----
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* ;-)