File.php 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. <?php
  2. /**
  3. * lvzheAdmin
  4. * @author xiekunyu <raingad@foxmail.com>
  5. * @license http://www.apache.org/licenses/LICENSE-2.0
  6. */
  7. namespace utils;
  8. //------------------------
  9. // 文件下载和图片下载
  10. //-------------------------
  11. class File
  12. {
  13. /**
  14. * 文件下载
  15. * @param $file_path
  16. * @param string $file_name
  17. * @param string $file_size
  18. * @param string $ext
  19. */
  20. public static function download($file_path, $file_name = '', $file_size = '', $ext = '')
  21. {
  22. if (!$file_name) {
  23. $file_name = basename($file_path);
  24. }
  25. if (!$file_size && in_array(substr($file_path, 0, 1), [".", "/"])) {
  26. try {
  27. $file_size = filesize($file_path);
  28. } catch (\Exception $e) {
  29. return "文件不存在";
  30. }
  31. }
  32. if (!$ext) {
  33. $ext = pathinfo($file_path, PATHINFO_EXTENSION);
  34. }
  35. if ($ext && !strpos($file_name, ".")) {
  36. $file_name = $file_name . "." . $ext;
  37. }
  38. $content_type = self::getMime($ext);
  39. header("Cache-Control:");
  40. header("Cache-Control: public");
  41. // 处理中文文件名
  42. $ua = $_SERVER["HTTP_USER_AGENT"];
  43. $encoded_filename = rawurlencode($file_name);
  44. // 文件类型
  45. if (preg_match("/Safari/", $ua)) {
  46. header('Content-Type: application/octet-stream;charset=utf-8');
  47. } else {
  48. header("Content-type: {$content_type}");
  49. }
  50. //IE
  51. if (preg_match("/MSIE/", $ua) || preg_match("/Trident\/7.0/", $ua)) {
  52. header('Content-Disposition: attachment; filename="' . $encoded_filename . '"');
  53. } else if (preg_match("/Firefox/", $ua)) {
  54. header('Content-Disposition: attachment; filename*="utf8\'\'' . $file_name . '"');
  55. } else if (preg_match("/Safari/", $ua)) {
  56. header('Content-Disposition: attachment; filename*=UTF-8\'\'' . $encoded_filename);
  57. } else {
  58. header('Content-Disposition: attachment; filename="' . $file_name . '"');
  59. }
  60. // 文件大小
  61. if ($file_size) {
  62. header("Accept-Length: " . $file_size);
  63. header("Content-Length: " . $file_size);
  64. }
  65. readfile($file_path);exit();
  66. }
  67. /**
  68. * 功能:php多种方式完美实现下载远程图片保存到本地
  69. * 参数:文件url,保存文件名称,使用的下载方式
  70. * 当保存文件名称为空时则使用远程文件原来的名称
  71. * @param string $url 请求图片的链接
  72. * @param string $filename 保存的文件名
  73. * @param int $type 保存图片的类型 0为curl,适用于静态图片,其他为缓冲缓存,适用于动态图片
  74. * @return string $filename 返回保存的文件名
  75. */
  76. public static function downloadImage($url, $filename, $type = 0)
  77. {
  78. if ($url == '') {
  79. return false;
  80. }
  81. $ext = pathinfo($filename, PATHINFO_EXTENSION);
  82. if (!in_array($ext, ['jpg', 'jpeg', 'png', 'gif', 'bmp', 'ico', 'tif', 'tiff'])) {
  83. $ext = pathinfo($url, PATHINFO_EXTENSION);
  84. if (!in_array($ext, ['jpg', 'jpeg', 'png', 'gif', 'bmp', 'ico', 'tif', 'tiff'])) {
  85. $ext = 'jpg';
  86. }
  87. $filename = $filename . "." . $ext;
  88. }
  89. //下载文件流
  90. if ($type) {
  91. $ch = curl_init();
  92. $timeout = 5;
  93. curl_setopt($ch, CURLOPT_URL, $url);
  94. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  95. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
  96. $img = curl_exec($ch);
  97. curl_close($ch);
  98. } else {
  99. ob_start();
  100. readfile($url);
  101. $img = ob_get_contents();
  102. ob_end_clean();
  103. }
  104. //保存文件
  105. // try {
  106. $fp2 = fopen($filename, 'w');
  107. fwrite($fp2, $img);
  108. fclose($fp2);
  109. return $filename;
  110. /*} catch (\think\Exception $e) {
  111. //TODO 异常处理
  112. return false;
  113. }*/
  114. }
  115. /**
  116. * 获取文件Mime
  117. * @param string $ext
  118. * @return string
  119. */
  120. public static function getMime($ext)
  121. {
  122. $mimes = [
  123. 'xml' => 'text/xml',
  124. 'json' => 'application/json',
  125. 'js' => 'text/javascript',
  126. 'php' => 'application/octet-stream',
  127. 'css' => 'text/css',
  128. 'html' => 'text/html',
  129. 'htm' => 'text/html',
  130. 'xhtml' => 'text/html',
  131. 'rss' => 'application/rss+xml',
  132. 'yaml' => 'application/x-yaml',
  133. 'atom' => 'application/atom+xml',
  134. 'pdf' => 'application/pdf',
  135. 'text' => 'text/plain',
  136. 'png' => 'image/png',
  137. 'jpg' => 'image/jpeg',
  138. 'gif' => 'image/gif',
  139. 'csv' => 'text/csv',
  140. 'tif' => 'image/tiff',
  141. 'ai' => 'application/postscript',
  142. 'asp' => 'text/asp',
  143. 'au' => 'audio/basic',
  144. 'avi' => 'video/avi',
  145. 'rmvb' => 'application/vnd.rn-realmedia-vbr',
  146. '3gp' => 'application/octet-stream',
  147. 'flv' => 'application/octet-stream',
  148. 'mp3' => 'audio/mpeg',
  149. 'wav' => 'audio/wav',
  150. 'sql' => 'application/octet-stream',
  151. 'rar' => 'application/octet-stream',
  152. 'zip' => 'application/zip',
  153. '7z' => 'application/octet-stream',
  154. 'bmp' => 'application/x-bmp',
  155. 'cdr' => 'application/x-cdr',
  156. 'class' => 'java/*',
  157. 'exe' => 'application/x-msdownload',
  158. 'fax' => 'image/fax',
  159. 'icb' => 'application/x-icb',
  160. 'ico' => 'image/x-icon',
  161. 'java' => 'java/*',
  162. 'jfif' => 'image/jpeg',
  163. 'jpeg' => 'image/jpeg',
  164. 'jsp' => 'text/html',
  165. 'mp4' => 'video/mpeg4',
  166. 'mpa' => 'video/x-mpg',
  167. 'mpeg' => 'video/mpg',
  168. 'mpg' => 'video/mpg',
  169. 'mpga' => 'audio/rn-mpeg',
  170. 'ras' => 'application/x-ras',
  171. 'tiff' => 'image/tiff',
  172. 'txt' => 'text/plain',
  173. 'wax' => 'audio/x-ms-wax',
  174. 'wm' => 'video/x-ms-wm',
  175. 'apk' => 'application/vnd.android.package-archive',
  176. 'doc' => 'application/msword',
  177. 'dot' => 'application/msword',
  178. 'docx' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
  179. 'dotx' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.template',
  180. 'docm' => 'application/vnd.ms-word.document.macroEnabled.12',
  181. 'dotm' => 'application/vnd.ms-word.template.macroEnabled.12',
  182. 'xls' => 'application/vnd.ms-excel',
  183. 'xlt' => 'application/vnd.ms-excel',
  184. 'xla' => 'application/vnd.ms-excel',
  185. 'xlsx' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
  186. 'xltx' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.template',
  187. 'xlsm' => 'application/vnd.ms-excel.sheet.macroEnabled.12',
  188. 'xltm' => 'application/vnd.ms-excel.template.macroEnabled.12',
  189. 'xlam' => 'application/vnd.ms-excel.addin.macroEnabled.12',
  190. 'xlsb' => 'application/vnd.ms-excel.sheet.binary.macroEnabled.12',
  191. 'ppt' => 'application/vnd.ms-powerpoint',
  192. 'pot' => 'application/vnd.ms-powerpoint',
  193. 'pps' => 'application/vnd.ms-powerpoint',
  194. 'ppa' => 'application/vnd.ms-powerpoint',
  195. 'pptx' => 'application/vnd.openxmlformats-officedocument.presentationml.presentation',
  196. 'potx' => 'application/vnd.openxmlformats-officedocument.presentationml.template',
  197. 'ppsx' => 'application/vnd.openxmlformats-officedocument.presentationml.slideshow',
  198. 'ppam' => 'application/vnd.ms-powerpoint.addin.macroEnabled.12',
  199. 'pptm' => 'application/vnd.ms-powerpoint.presentation.macroEnabled.12',
  200. 'potm' => 'application/vnd.ms-powerpoint.template.macroEnabled.12',
  201. 'ppsm' => 'application/vnd.ms-powerpoint.slideshow.macroEnabled.12',
  202. ];
  203. return isset($mimes[$ext]) ? $mimes[$ext] : 'application/octet-stream';
  204. }
  205. /**
  206. * 下载文件到服务器
  207. * addtime 2020年8月28日 18:38:43
  208. */
  209. public static function getFile($url, $save_dir = '', $filename = '', $type = 0)
  210. {
  211. if (trim($url) == '') {
  212. return false;
  213. }
  214. if (trim($save_dir) == '') {
  215. $save_dir = './';
  216. }
  217. if (0 !== strrpos($save_dir, '/')) {
  218. $save_dir .= '/';
  219. }
  220. //创建保存目录
  221. if (!file_exists($save_dir) && !mkdir($save_dir, 0777, true)) {
  222. return false;
  223. }
  224. //获取远程文件所采用的方法
  225. if ($type) {
  226. $ch = curl_init();
  227. $timeout = 5;
  228. curl_setopt($ch, CURLOPT_URL, $url);
  229. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  230. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
  231. $content = curl_exec($ch);
  232. curl_close($ch);
  233. } else {
  234. ob_start();
  235. readfile($url);
  236. $content = ob_get_contents();
  237. ob_end_clean();
  238. }
  239. $size = strlen($content);
  240. //文件大小
  241. $fp2 = @fopen($save_dir . $filename, 'a');
  242. fwrite($fp2, $content);
  243. fclose($fp2);
  244. unset($content, $url);
  245. $res['code'] = 200;
  246. $res['file_name'] = $filename;
  247. return $res;
  248. }
  249. /**
  250. * 检测文件大小
  251. */
  252. public static function getFileSize($url)
  253. {
  254. $res = get_headers($url, true);
  255. $filesize = round($res['Content-Length'] / 1024 / 1024, 2); //四舍五入获取文件大小,单位M
  256. return $filesize;
  257. }
  258. }