Reputation: 596
I have a php script that connects to a certain server and downloads the file from that server. Now I'm trying my best to hide this download location so that the user cannot find out where the hidden folder is (on the remote server).
So what I did was try the readfile(url) method, this worked like a charm except it was extremely slow. I'm guessing that Readfile first downloads the file in some form into my server from the remote server and to the client, thats why its slower?
I was hoping there might be another solution out there that can just act as an intermediary without disturbing the download speed.
@EDIT------- Tried cURL but speed still suffers------
$ch = curl_init($url);
$filename='newfile.rar';
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private",false);
header("Content-Type: application/octet-stream");
header("Content-Disposition: attachment; filename=\"".$filename."\";");
header("Content-Transfer-Encoding: binary");
header('Content-Length: '.'20000000');
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);// allow redirects
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); // return into a variable
curl_setopt($ch, CURLOPT_WRITEFUNCTION, 'write_callback');
curl_exec($ch);
function write_callback($ch, $data) {
echo $data;
flush();
ob_flush();
print_r(curl_getinfo($ch));
return strlen($data);
Upvotes: 0
Views: 269
Reputation: 163603
You can make a simple proxy with cURL by using its various callbacks.
http://curl.haxx.se/libcurl/c/curl_easy_setopt.html
Basically, you'll want to specify the CURLOPT_WRITEFUNCTION
option value to be a function that echos that data to the client. Don't forget to flush buffers when doing this.
That way, while your server downloads the file, it will be simultaneously sending it to the client.
More info here: http://php.net/manual/en/function.curl-setopt.php
Edit: I guess an example is in order. Note, I haven't tested this code, but this should get you started...
$ch = curl_init("http://www.whatever.com/something");
curl_setopt($ch, CURLOPT_WRITEFUNCTION, 'write_callback');
function write_callback($ch, $data) {
echo $data;
flush();
ob_flush();
return strlen($data);
}
Upvotes: 1
Reputation: 592
You can use cURL to do this. Here is a basic example:
$ch = curl_init("http://www.domain.com/file.php");
curl_exec($ch);
This will call the url, and load it into the current browser. If you just want to store this data in a variable you can do the following:
$ch = curl_init("http://www.domain.com/file.php");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($ch);
Upvotes: 0