php curl下载文件 [英] php curl to download files

查看:248
本文介绍了php curl下载文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要通过curl下载文件并将其保存在服务器上。第二次,我将点击文件下载URL,我应该在自己的服务器上启动。
我的代码如下:

I need to download files via curl and save them on the server. and the second time, i will hit the file download url, i should start on my own server. My code below:

$output_filename = "file.zip";
$host = $_GET['link'];

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $host);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_AUTOREFERER, false);
curl_setopt($ch, CURLOPT_REFERER, "http://your-site.url");
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_2_0);
curl_setopt($ch, CURLOPT_HEADER, 0);
$agents = array(
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.7; rv:7.0.1) Gecko/20100101     Firefox/7.0.1',);
curl_setopt($ch,CURLOPT_USERAGENT,$agents[array_rand($agents)]);
$result = curl_exec($ch);
curl_close($ch);

header('Content-Disposition: attachment; filename ="file.zip"');
print_r($result); // prints the contents of the collected file before writing..


推荐答案

如果我认为这是正确的,那么您想构建类似缓存的内容,从远程服务器下载一次文件x,然后再从您自己的服务器提供文件?

If i interpret this correct you want to build something like a cache where you download file x once from a remote server and after that the file should be served from your own server ?

如果是这种情况,这可能是解决问题的好方法:

If that is the case this could be a good approach for the problem:

class Downloader
{

    private $path;
    public $filename;

    public function __construct()
    {
        $this->path = dirname(__FILE__);
    }

    public function getFile($url)
    {
        $fileName = $this->getFileName($url);
        if(!file_exists($this->path."/".$fileName)){
            $this->downloadFile($url,$fileName);
        }

        return file_get_contents($this->path."/".$fileName);
    }


    public function getFileName($url)
    {
        $slugs = explode("/", $url);
        $this->filename = $slugs[count($slugs)-1];
        return $this->filename;
    }

    private function downloadFile($url,$fileName)
    {
        //This is the file where we save the information
        $fp = fopen ($this->path.'/'.$fileName, 'w+');
        //Here is the file we are downloading, replace spaces with %20
        $ch = curl_init(str_replace(" ","%20",$url));
        curl_setopt($ch, CURLOPT_TIMEOUT, 50);
        // write curl response to file
        curl_setopt($ch, CURLOPT_FILE, $fp); 
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
        // get curl response
        curl_exec($ch); 
        curl_close($ch);
        fclose($fp);
    }

}


$downloader = new Downloader();
$content = $downloader->getFile("YOUR URL");

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

这是一个非常基本的概念,有很多地方需要针对您的用例进行改进和定制

This is a very basic concept there are many point this have to be improved and customized for your use case.

这篇关于php curl下载文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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