How do I use my server as a proxy to download files via PHP?

前端 未结 2 1163
无人及你
无人及你 2021-02-06 14:52

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 s

相关标签:
2条回答
  • 2021-02-06 15:13
            $fp = fopen($url, 'rb');
            foreach (get_headers($url) as $header)
            {
                header($header);
            }
    
            fpassthru($fp);
            exit;
    

    This will simply download a remote file to the browser with correct headers.

    0 讨论(0)
  • 2021-02-06 15:29

    Very very simply like this:

    $url = $_GET['file'];
    $path_parts = pathinfo($url);
    
    $ext = $path_parts['extension'];
    $filename = $path_parts['filename'];
    
    header("Content-type: application/$ext");
    header("Content-Disposition: attachment; filename=$filename");
    
    echo file_get_contents($url);
    

    If the file is larger than a few megabytes, use fopen fread and frwrite download the file in chunks and send to the client in chunks.

    0 讨论(0)
提交回复
热议问题