Commit 35022387 authored by jiangkebao's avatar jiangkebao

Merge branch 'master_download_file' into test

parents 85d38a53 ed5f9795
...@@ -814,3 +814,61 @@ if (!function_exists('get_images_url')) { ...@@ -814,3 +814,61 @@ if (!function_exists('get_images_url')) {
return $match; return $match;
} }
} }
if (!function_exists('download_file')) {
/**
* 下载远程文件
* @param $url 远程文件路径
* @param null $path 系统保存路径
* @return string
*/
function download_file($url, $path = null)
{
$filename = trim(pathinfo($url, PATHINFO_FILENAME));
$ext = strtolower(pathinfo($url, PATHINFO_EXTENSION));
$filename = "$filename.$ext";
$root = config('server.settings.document_root', BASE_PATH . '/public');
$path = $path ? $path : '/download/files';
$savePath = $root . $path;
if (!is_dir($savePath)) {
// 判断路径是否存在,不存在,则创建
mkdir($savePath, 0777, true);
}
$filePath = $savePath . '/' . $filename;
if (!file_exists($filePath)) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
$file = curl_exec($ch);
curl_close($ch);
$resource = fopen($filePath, 'a');
fwrite($resource, $file);
fclose($resource);
}
return $path . '/' . $filename;;
}
}
if (!function_exists('download_file_stream')) {
/**文件流保存到本地
* @param $fileStream 文件流
* @param null $path 保存路径
* @return string
*/
function download_file_stream($fileStream, $path = null)
{
$root = config('server.settings.document_root', BASE_PATH . '/public');
$path = $path ? $path : '/download/files';
$savePath = $root . $path;
if (!is_dir($savePath)) {
// 判断路径是否存在,不存在,则创建
mkdir($savePath, 0777, true);
}
$fileName = '/' . date('YmdHis') . 'track' . '.pdf';
file_put_contents($savePath . $fileName, base64_decode($fileStream), 1);
return $path . $fileName;
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment