php实现从服务器下载文件
<?php
/*
*@Description:往此方法传三个参数,获取服务器的文件
*
*/
class getServerFile{
public function getServerFile($url = "", $file = "", $timeout = 60){
$file = empty($file) ? $file.pathinfo($url, PATHINFO_BASENAME) : $file;
//$file = $file.pathinfo($url, PATHINFO_BASENAME);
$dir = pathinfo($file, PATHINFO_DIRNAME);
!is_dir($dir) && @mkdir($dir, 0755, 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($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)){
//$http_response_header
return $file;
}else{
return false;
}
}
}
}
//实际参数没有填写
$url = "";
$file = "";
$getServerFile = new getServerFile();
$filename = $getServerFile-> getServerFile();
/*
*@Description:往此方法传三个参数,获取服务器的文件
*
*/
class getServerFile{
public function getServerFile($url = "", $file = "", $timeout = 60){
$file = empty($file) ? $file.pathinfo($url, PATHINFO_BASENAME) : $file;
//$file = $file.pathinfo($url, PATHINFO_BASENAME);
$dir = pathinfo($file, PATHINFO_DIRNAME);
!is_dir($dir) && @mkdir($dir, 0755, 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($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)){
//$http_response_header
return $file;
}else{
return false;
}
}
}
}
//实际参数没有填写
$url = "";
$file = "";
$getServerFile = new getServerFile();
$filename = $getServerFile-> getServerFile();
?>
来源地址:http://www.aitiblog.com/php/367.html
声明:该文观点仅代表作者本人,牛骨文系教育信息发布平台,牛骨文仅提供信息存储空间服务。