猎户素材采集系统

FileService.php 1.0KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. <?php
  2. namespace App\Service;
  3. class FileService
  4. {
  5. /*
  6. * 下载多媒体资源到本地
  7. * */
  8. public static function mediaDownload($url)
  9. {
  10. // $save_dir = '/mnt/queryList/upload/';
  11. $save_dir = '/public/upload/';
  12. $files = explode('/', $url);
  13. $fileName = $files[count($files)-1];
  14. //创建保存目录
  15. if(!file_exists($save_dir)) {
  16. if(!mkdir($save_dir, 0777, true)) {
  17. // Log::logError('创建目录失败,目录路径:'.$save_dir);
  18. }
  19. }
  20. //获取远程文件所采用的方法
  21. $ch = curl_init();
  22. $timeout = 5;
  23. curl_setopt($ch, CURLOPT_URL, $url);
  24. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  25. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
  26. $content = curl_exec($ch);
  27. curl_close($ch);
  28. $fp2 = @fopen($save_dir . $fileName, 'a');
  29. fwrite($fp2, $content);
  30. fclose($fp2);
  31. unset($content, $url);
  32. return realpath($save_dir . $fileName);
  33. }
  34. }