Reputation: 61
Basically I want to download a file from an external host and pass it directly to the user without having to be saved on the server, in practice, act as a proxy for this file, so that the request is always made from my server and not the user. Should I simulate this request:
GET / servername / filename.ext HTTP/1.1 (any large file)
Host: namehost
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv: 27.0) Gecko/20100101 Firefox/27.0
Accept: text / html, application / xhtml + xml, application / xml; q = 0.9, * / * q = 0.8
Accept-Language: en-us, en; q = 0.8, en-US; q = 0.5, en; q = 0.3
Accept-Encoding: gzip, deflate
Referer: sitename / ...
Cookie: ....
Connection: keep-alive
I already have the necessary cookies, and all the necessary headers but I can not start the download, I tried using different scripts in Curl, but the download does not start.
Can anyone help me please.
Upvotes: 5
Views: 7457
Reputation: 1104
You want to fetch the file from remote server and serve it to your user. Below is the fetch and serve proxy sample code. I am expecting that you know the file name, URL, file extension and mime type
<?php
function get_size($url) {
$my_ch = curl_init();
curl_setopt($my_ch, CURLOPT_URL,$url);
curl_setopt($my_ch, CURLOPT_HEADER, true);
curl_setopt($my_ch, CURLOPT_NOBODY, true);
curl_setopt($my_ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($my_ch, CURLOPT_TIMEOUT, 10);
$r = curl_exec($my_ch);
foreach(explode("\n", $r) as $header) {
if(strpos($header, 'Content-Length:') === 0) {
return trim(substr($header,16));
}
}
return '';
}
// Set operation params
$mime = filter_var($_GET['mime']);
$ext = str_replace(array('/', 'x-'), '', strstr($mime, '/'));
$url = base64_decode(filter_var($_GET['url']));
$name = urldecode($_GET['title']). '.' .$ext;
// Fetch and serve
if ($url)
{
$size=get_size($url);
// Generate the server headers
if (strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE') !== FALSE)
{
header('Content-Type: "' . $mime . '"');
header('Content-Disposition: attachment; filename="' . $name . '"');
header('Expires: 0');
header('Content-Length: '.$size);
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header("Content-Transfer-Encoding: binary");
header('Pragma: public');
}
else
{
header('Content-Type: "' . $mime . '"');
header('Content-Disposition: attachment; filename="' . $name . '"');
header("Content-Transfer-Encoding: binary");
header('Expires: 0');
header('Content-Length: '.$size);
header('Pragma: no-cache');
}
readfile($url);
exit;
}
// Not found
exit('File not found');
?>
Usage: simplly save it as download.php
and call like
$encoded_url = base64_encode($file_to_download_url);
$download_url = 'http://www.example.com/download.php?mime='.$mime.'&title='.$title.'&url='.$encoded_url;
It will work like a charm!
Upvotes: 5
Reputation: 27
You can simply put the URI of the file that you want to download a the value for the hyperlink:
<a href="URI" target="_blank">Click to download</a>
If you don't want them to click anything, and have the file download right away, you could use the php header() function. See example 1 here: http://uk.php.net/manual/en/function.header.php
Upvotes: 0