如何使用我的服务器作为代理通过PHP下载文件? [英] How do I use my server as a proxy to download files via PHP?

查看:132
本文介绍了如何使用我的服务器作为代理通过PHP下载文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要我的服务器作为第三方服务器(文件最初所在的位置)与最终用户之间的代理。也就是说,我的服务器从第三方服务器下载文件,按顺序,用户从我的服务器下载文件。这将导致带宽为文件大小的两倍。如何使用PHP实现此过程?

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 PHP?

推荐答案

非常简单的这样:

$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);

如果文件大于几兆字节,请使用fopen fread和frwrite以块为单位下载文件,以块形式发送给客户。

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.

这篇关于如何使用我的服务器作为代理通过PHP下载文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆