Transfer from Server > Server > Client without download to server
Here's my case: I have a file on a server (e.g www.server1.com/file.rar). I want send this file to my client. But, the www.server1.com accept connections from only one IP. I have a lot of clients, so I have a lot of IP's.
The solution could be transfer the file from www.server1.com to www.server2.com (which has one IP address only) and provide the file: www.server2.com/file.r开发者_运维百科ar But... In this case I need transfer the file from server1 to server2, and that's (bandwidth and space).
There's any alternatives for that? I was thinking about something like:
header("Content-Disposition: attachment; filename=" . urlencode($file));
header("Content-Type: application/force-download");
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");
header("Content-Description: File Transfer");
file_get_contents("http://server1.com/file.rar");
But didn't work as expected.
Thanks in advance
Edit: I can't reply my own question cause I'm new user... Well, i want say thanks to Everyone for the replys, they send me to the solution. Which is: file_get_contents didn't work because of server flush, so I used fopen and fread with ob_flush and flush
header("Content-Disposition: attachment; filename=" . urlencode($arr[1])); //$arr[1]
header("Content-Type: application/force-download");
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");header("Content-Description: File Transfer");
ob_flush();
flush();
$fp = fopen($url, "r");//$url variable with file url
while (!feof($fp)) {
echo fread($fp, 65536);
ob_flush();
flush();
}
fclose($fp);
Thanks!
here some example code. ive not tested it tho but think it would work. using cURL and mod rewrite hides true location of file.
<?php
function downloader($url){
$file_name = basename($url);
$generated_file = geturl($url, $url);
file_put_contents($file_name,$generated_file);
$size=strlen($generated_file);
if($size==0){die("<b>Error:</b>Location to download not found!");}
header('Content-type: application/force-download');
header('Content-Disposition: attachment; filename=' . $file_name);
header('Content-Transfer-Encoding: binary');
header('Expires: Sat, 26 Jul 1997 05:00:00 GMT');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: ' . $size);
readfile($file_name);
unlink($file_name);
}
function geturl($url, $referer) {
$headers[] = 'Accept: image/gif, image/x-bitmap, image/jpeg, image/pjpeg';
$headers[] = 'Connection: Keep-Alive';
$headers[] = 'Content-type: application/x-www-form-urlencoded;charset=UTF-8';
$user_agent = 'ONLY_MY_HOST';
$process = curl_init($url);
curl_setopt($process, CURLOPT_HTTPHEADER, $headers);
curl_setopt($process, CURLOPT_HEADER, 0);
curl_setopt($process, CURLOPT_USERAGENT, $useragent);
curl_setopt($process, CURLOPT_REFERER, $referer);
curl_setopt($process, CURLOPT_TIMEOUT, 30);
curl_setopt($process, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($process, CURLOPT_FOLLOWLOCATION, 1);
$return = curl_exec($process);
curl_close($process);
return $return;
}
downloader('www.server2.com/file.rar','http://www.server1.com');
?>
-CONTENT SERVER PART
<?php
/*
.htaccess
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^([^\.]+)$ serve.php?file=$1 [NC,L]
*/
//serve.php
if($_SERVER['HTTP_REFERER']=='http://www.server1.com' && $_SERVER['HTTP_USER_AGENT'] =='ONLY_MY_HOST' && $_SERVER['REMOTE_ADDRESS']=='123.123.123.123'){
readfile('./file_store/'.$_REQUEST['file']);
die();
}
header('Location: http://www.server1.com');
?>
You can use readfile (see http://php.net/manual/en/function.readfile.php). Note that readfile will only work if your server is properly configured (as discussed at PHP readfile() of external URL).
Alternatively you can use echo file_get_contents("http://server1.com/file.rar");
.
Use readfile
instead of file_get_contents
. Readfile will send the file to the client in the same rate it can download it from server1. that way you don't need to keep the file in memory (as with file_get_contents) and your client can start her download before you have fetched the entire file.
Notice that this will download the file to your server but not save it to disk, but instead send it out to your client. Thus the bandwidth usage will be there, but not the disk usage. There's no way for you to solve this without downloading the file from server1 to server2.
If you cache the file on server2's disk, you'll save bandwidth usage on the expense of disk.
You can either save bandwidth to server2 by caching the file to server2's disk, or you can save space on server2's disk by always downloading the file from server1 whenever the client requests it. There is no way to do both simultaneously.
精彩评论