FileHelper.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Administrator
  5. * Date: 2022/8/10
  6. * Time: 18:50
  7. */
  8. namespace file;
  9. use network\CurlHelper;
  10. use network\IpHelper;
  11. class FileHelper
  12. {
  13. /**
  14. * 读取文件文字内容
  15. * @param $filePath
  16. * @return mixed
  17. */
  18. public static function read($filePath)
  19. {
  20. return file_get_contents($filePath);
  21. }
  22. /**
  23. * 创建并保存文件
  24. * @param $filePath [指定目录]
  25. * @param $content [文字内容]
  26. * @throws \Exception
  27. */
  28. public static function save($filePath, $content)
  29. {
  30. if (file_exists($filePath)) {//file_exists检查文件是否存在
  31. $File = new \think\template\driver\File();
  32. $File->write($filePath, $content);
  33. } else {
  34. DirHelper::makeDir(PathHelper::getDir($filePath));//创建目录
  35. if (false == file_put_contents($filePath, $content) && !empty($content)) {
  36. throw new \Exception('创建文件失败,请检查目录权限');
  37. }
  38. }
  39. }
  40. /**
  41. * 重命名文件
  42. * @param $old_path
  43. * @param $new_path
  44. * @param $overWrite [如果新的文件已存在,false提示异常,true覆盖]
  45. * @return bool
  46. * @throws \Exception
  47. */
  48. public static function rename($old_path, $new_path, $overWrite = false)
  49. {
  50. $old_path = replaceDS($old_path);
  51. $new_path = replaceDS($new_path);
  52. if (!file_exists($old_path)) {
  53. throw new \Exception("old_path not exits:" . $old_path);
  54. }
  55. if (file_exists($new_path)) {
  56. if ($overWrite) {
  57. unlink($new_path);
  58. } else {
  59. throw new \Exception("new_path exits:" . $new_path);
  60. }
  61. }
  62. @rename($old_path, $new_path);
  63. return true;
  64. }
  65. /**
  66. * 输出文件到浏览器下载
  67. * @param $filepath [文件物理路径]
  68. */
  69. public static function downloadFile($filepath)
  70. {
  71. if (file_exists($filepath)) {
  72. $filename = PathHelper::getFilename($filepath);
  73. //打开文件
  74. $file1 = fopen($filepath, "r");
  75. //输入文件标签
  76. Header("Content-type: application/octet-stream");
  77. Header("Accept-Ranges: bytes");
  78. Header("Accept-Length: " . filesize($filepath));
  79. Header("Content-Disposition: attachment; filename=$filename");
  80. echo fread($file1, filesize($filepath));
  81. fclose($file1);
  82. }
  83. echo "文件不存在,请联系客服处理!";
  84. }
  85. /**
  86. * 采集文件
  87. * @param $downlink [文件下载地址]
  88. * @param $savePath [文件保存目录路径]
  89. * @param $saveName [文件保存名称]
  90. * @return string [返回文件物理路径]
  91. */
  92. public static function fetchDownFile($downlink, $savePath, $saveName)
  93. {
  94. DirHelper::makeDir($savePath);
  95. $loaclFile = $savePath . $saveName;
  96. if (startWith($downlink, 'https')) {
  97. $user_agent = 'Mozilla/5.0 (compatible; Baiduspider/2.0; +http://www.baidu.com/search/spider.html)';
  98. $referer = 'https://www.baidu.com/s?wd=%E7%99%BE%E5%BA%A6&rsv_spt=1&rsv_iqid=0xe5a39f3b0003c303&issp=1&f=8&rsv_bp=0&rsv_idx=2&ie=utf-8&tn=baiduhome_pg&rsv_enter=1&rsv_sug3=6&rsv_sug1=4&rsv_sug7=100';
  99. $randip = IpHelper::getRandIp();
  100. $arrContextOptions = [
  101. 'ssl' => [
  102. 'verify_peer' => false,
  103. 'verify_peer_name' => false,
  104. ],
  105. 'http' => [
  106. 'method' => "GET",
  107. 'header' => "User-Agent: {$user_agent}\r\nReferer: {$referer}\r\nX-FORWARDED-FOR: {$randip}\r\nCLIENT-IP: {$randip}\r\n",
  108. 'timeout' => 10,
  109. ]
  110. ];
  111. //下载文件忽略ssl
  112. $response = file_get_contents($downlink, false, stream_context_create($arrContextOptions)) || die('服务器不支持采集');
  113. file_put_contents($loaclFile, $response);
  114. } else {
  115. ob_start(); //打开输出
  116. readfile($downlink); //输出图片文件
  117. $img = ob_get_contents(); //得到浏览器输出
  118. ob_end_clean(); //清除输出并关闭
  119. file_put_contents($loaclFile, $img);
  120. }
  121. return $loaclFile;
  122. }
  123. /**
  124. * 采集文件
  125. * @param $downlink [文件下载地址]
  126. * @param $savePath [文件保存目录路径]
  127. * @param $saveName [文件保存名称]
  128. * @return string [返回文件物理路径]
  129. */
  130. public static function fetchDownFile2($downlink, $savePath, $saveName)
  131. {
  132. DirHelper::makeDir($savePath);
  133. $loaclFile = $savePath . $saveName;
  134. $curl = new CurlHelper($downlink);
  135. $randip = IpHelper::getRandIp();
  136. $curl->setHeader([
  137. 'CLIENT-IP:' . $randip,
  138. 'X-FORWARDED-FOR:' . $randip
  139. ]);
  140. $curl->setReferer("https://www.baidu.com/s?wd=%E7%99%BE%E5%BA%A6&rsv_spt=1&rsv_iqid=0xe5a39f3b0003c303&issp=1&f=8&rsv_bp=0&rsv_idx=2&ie=utf-8&tn=baiduhome_pg&rsv_enter=1&rsv_sug3=6&rsv_sug1=4&rsv_sug7=100");
  141. $curl->exec();
  142. $response = $curl->getResponse();
  143. if ($response && 200 == $curl->getHttpCode() && !$curl->getError()) {
  144. file_put_contents($loaclFile, $response);
  145. }
  146. return $loaclFile;
  147. }
  148. }