源码论坛,商业源码下载,尽在锦尚中国商业源码论坛
标题:
PHP实现远程下载文件到本地的办法
[打印本页]
作者:
洪七公
时间:
2023-1-10 15:45
标题:
PHP实现远程下载文件到本地的办法
单个文件举个例子,多文件的话可以放在一个文件夹下遍历后提供下载:
public function aaa(){
$ddd = "http://47.105.151.181:8012/5fce06e407657/3.jpg";
$ccc = "http://www.baidu.com/img/baidu_sylogo1.gif";
$this->httpcopy($ddd);
}
public function httpcopy($url, $file="", $timeout=60) {
$file = empty($file) ? pathinfo($url,PATHINFO_BASENAME) : $file;
$dir = "Upload/docConversionImage/5fce06e407657/";
if (!file_exists($dir)){
mkdir ($dir,0777,true);
}
$url = str_replace(" ","%20",$url);
if(function_exists('curl_init')) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$temp = curl_exec($ch);
if(@file_put_contents($dir.$file, $temp) && !curl_error($ch)) {
return $file;
}else{
return false;
}
}else{
$opts = array(
"http"=>array(
"method"=>"GET",
"header"=>"",
"timeout"=>$timeout
)
);
$context = stream_context_create($opts);
if(@copy($url, $file, $context)) {
return $file;
}else{
return false;
}
}
}
复制代码
欢迎光临 源码论坛,商业源码下载,尽在锦尚中国商业源码论坛 (https://bbs.52jscn.com/)
Powered by Discuz! X3.3