PHP proxy to download files

php download file
proxy php file
fpassthru
php stream file
mini proxy php
php proxy file upload
php api proxy file
php proxy github

I think this is a easy one.

I have a simple website with a reserved area and some files to download from it.

I disabled direct file download with .htaccess, and I manage the download of file via this simple proxy.php file:

// ....

if ($filename === null || !file_exists($proxiedDirectory.$filename)) {
    http_response_code(404);
    exit;
}

if (!$site->is_logged_in()) { 
    http_response_code(403);
    exit;
}

$fp = fopen($proxiedDirectory.$filename, 'rb');

header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="'.$filename.'"');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($filename));
fpassthru($fp);
exit;

This code WORKS perfectly, but unfortunately I have a 1.2Gb file that users could download, and this script is too slow, and doesn't allow the full file download.

Any help would be appreciated,

Thanks in advance!

M.

You can use a combination of header(), set_time_limit(), fgets(), ob_flush(), flush(). Here is my example, best using Php 64bit on OS 64bit because filesize() has not limits in this architecture.

    // Set force download headers
    header('Content-Description: File Transfer');
    header('Content-Type: application/octet-stream');
    header('Content-Disposition: attachment; filename="' . $file . '"');
    header('Content-Transfer-Encoding: binary');
    header('Connection: Keep-Alive');
    header('Expires: 0');
    header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
    header('Pragma: public');
    header('Content-Length: ' . sprintf("%u", filesize($downloads_folder . $file)));
    // Open and output file contents
    set_time_limit(0);
    $fh = fopen($downloads_folder . $file, "rb");
    while (!feof($fh)) {
      echo fgets($fh);
      ob_flush();
      flush();
    }
    fclose($fh);
    exit;

Hope this helps.

mcnemesis/proxy.php: A Single-File PHP-based proxy that , Download or clone this repo and copy the config.php and proxy.php files into the root web folder (or wherever it is you want to deploy the proxy). Downloading files with cURL in PHP This is a simple tutorial on how to download files with cURL in PHP. Firstly, allow me to present you with the code (because let’s be honest, that’s what most of you came here for).

Use chunked download, smth like this:

$chunkSize = 1024 * 1024;
while (!feof($fp))
{
    $buffer = fread($fp, $chunkSize);
    echo $buffer;
    ob_flush();
    flush();
}

Download File List - PHP-proxy, PHP-Proxy is a PHP script for taking Web pages from one server, so that your main server is protected if the Web server is overloaded from things like DOS� PHP File Download. In this tutorial you will learn how to force download a file using PHP. Downloading Files with PHP. Normally, you don't necessarily need to use any server side scripting language like PHP to download images, zip files, pdf documents, exe files, etc.

use nginx as proxy may be more suitable. The reason is simple : when you download by php, there will be timeout when request in php-fpm, so the file more likely can not download complete.

location /proxy.lua {
    proxy_pass http://proxy.com/link/;
} 

if you need to check user has login or not, you can use lua + nginx(OpenResty)

yet there is a simple way to check that: 1. proxy.php redirect the request to nginx location proxy.lua with two parameter: ts and code.

ts is timestamp of server
code is md5sum(ts+"complexstring")
header("Location: /proxy.lua?ts=122&code=xxx&filename=xxxx", 302);
exit(0);

when in lua:

parse the parameter ts and code, validate the code and ts value
proxy the file

Creating a Streaming Proxy with PHP, This works perfectly for the problem in hand; the music files are small, so you've got no problems with memory consumption, and the downloads� Download PHP Web Proxy for free. PHP Http Proxy is a php script for taking webpages from one server and processes so that your main server is proctected/hidden. Usefull for those who have or require indirect access to the web and or their server.

Use ob_end_clean() before fpassthru($fp)

e.g.

ob_end_clean();
fpassthru($fp);

This might work for large files

fpassthru - Manual, Oddly enough, all the downloads seemed to work ok, but the files were corrupted: If the client (for example an old HTTP proxy) is using HTTP/1.0, it may not� Tip. A URL can be used as a filename with this function if the fopen wrappers have been enabled. See fopen() for more details on how to specify the filename. See the Supported Protocols and Wrappers for links to information about what abilities the various wrappers have, notes on their usage, and information on any predefined variables they may provide.

header - Manual, Many proxies and clients can be forced to disable caching with: If using the ' header' function for the downloading of files, especially if you're passing the� I need my server to act as a proxy between a 3rd party server (where the file is originally located) and the end user. That is, my server downloads the file from the 3rd party server, and sequentially, the user downloads it from my server. This should result in an incurred bandwidth of twice the file size. How can this process be achieved using

cURL - Manual, I needed to use cURL in a php script to download data using not only SSL for the In order to use curl with secure sites you will need a ca-bundle.crt file; here's a if ($this->proxy) curl_setopt($process, CURLOPT_PROXY, $this->proxy); Download links for GatherProxy 9.0. Lists and checks thousands of proxy servers. See your internet history, even if the history, cache, temp files have been

FTP context options - Manual, File offset at which to begin transfer. Applies to read mode (downloading) only. Defaults to 0 (Beginning of File). proxy string. Proxy FTP request via http proxy� PHP levels are 0 (writes messages and errors to logs), 1 (shows proxy errors and messages in browser console), 2 (combination of levels 0 and 1), and 3 (no logging); the default is 0. Add a new <serverUrl> entry for each service that will use the proxy.

Comments
  • Thanks ryan, your answer is correct, but unfortunately I have a shared apache host :(
  • you can redirect the request to some host which is nginx or openresty. When proxy a large file or the client network is not fast enough, timeout will be occurred definitely.