1234567891011121314151617181920212223242526272829303132333435363738394041 |
- <?php
- namespace App\Service;
- class FileService
- {
- /*
- * 下载多媒体资源到本地
- * */
- public static function mediaDownload($url)
- {
- // $save_dir = '/mnt/queryList/upload/';
- $save_dir = '/public/upload/';
- $files = explode('/', $url);
- $fileName = $files[count($files)-1];
- //创建保存目录
- if(!file_exists($save_dir)) {
- if(!mkdir($save_dir, 0777, true)) {
- // Log::logError('创建目录失败,目录路径:'.$save_dir);
- }
- }
- //获取远程文件所采用的方法
- $ch = curl_init();
- $timeout = 5;
- curl_setopt($ch, CURLOPT_URL, $url);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
- curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
- $content = curl_exec($ch);
- curl_close($ch);
- $fp2 = @fopen($save_dir . $fileName, 'a');
- fwrite($fp2, $content);
- fclose($fp2);
- unset($content, $url);
- return realpath($save_dir . $fileName);
- }
- }
|