简体   繁体   English

使用控制器上的curl直接下载

[英]Direct download using curl on a controller

I'm trying to download a file directly from a link but I need to use the Curl, the direct link is not valid, no security restrictions on the server, for direct access. 我正在尝试直接从链接下载文件,但是我需要使用Curl,直接链接无效,服务器上没有安全限制,可以直接访问。

Just need to access the link via curl and perform the download. 只需通过curl访问链接并执行下载。

Use something like this: 使用这样的东西:

/ * php script * / / * PHP脚本* /

$ url = "http://server:8080/system/downloads/idfile"; 
$ ch = curl_init (); 
curl_setopt ($ ch, CURLOPT_URL, $ url); 
curl_setopt ($ ch, CURLOPT_FOLLOWLOCATION, true); 
curl_setopt ($ ch, CURLOPT_HEADER, 0); 
curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, true); 
curl_setopt ($ ch, CURLOPT_BINARYTRANSFER, true); 
$ data = curl_exec ($ ch); 
curl_close ($ ch); 
return $ data; 

/ * end script * / / *结束脚本* /

I used some headers before return, but file format doc, xls, ppt return corrupted 我在返回之前使用了一些标头,但是文件格式doc,xls,ppt返回已损坏

header ("Content-type: application/msword"); 
header ("Content-Disposition: attachment; filename=file.doc"); 

Could help me to download the file directly by clicking button in an html page and run the script curl up? 可以通过单击html页面中的按钮来帮助我直接下载文件并运行脚本吗?

Currently I am using codeigniter where the curl of this script in a controller , and action will be implemented through a link in a view 当前,我正在使用codeigniter,其中该脚本在控制器中的卷曲,并且将通过视图中的链接来实现操作

thanks 谢谢

You can use this am always using this code. 您可以始终使用此代码来使用它。

function get_all_data($url) {
    $ch = curl_init();
    $timeout = 5;
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
    $data = curl_exec($ch);
    curl_close($ch);
    return $data;
}
$returned_content = get_all_data('https://www.connectedcommerce.net/');
file_put_contents('open.txt',$returned_content,FILE_APPEND);

That way did not work. 这样行不通。 There is no data returned. 没有返回数据。 The link to access the call is a method to download from another system, and so the use of restricted direct access to the link. 访问呼叫的链接是从另一个系统下载的一种方法,因此使用受限的直接访问链接。 Smpre I call the method through the curl, but not to any return. smpre我通过curl调用了该方法,但是没有返回任何结果。 The download method works because the tested 下载方法有效,因为经过测试

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM