123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158 |
- <?php
- namespace file;
- use network\CurlHelper;
- use network\IpHelper;
- class FileHelper
- {
-
- public static function read($filePath)
- {
- return file_get_contents($filePath);
- }
-
- public static function save($filePath, $content)
- {
- if (file_exists($filePath)) {
- $File = new \think\template\driver\File();
- $File->write($filePath, $content);
- } else {
- DirHelper::makeDir(PathHelper::getDir($filePath));
- if (false == file_put_contents($filePath, $content) && !empty($content)) {
- throw new \Exception('创建文件失败,请检查目录权限');
- }
- }
- }
-
- public static function rename($old_path, $new_path, $overWrite = false)
- {
- $old_path = replaceDS($old_path);
- $new_path = replaceDS($new_path);
- if (!file_exists($old_path)) {
- throw new \Exception("old_path not exits:" . $old_path);
- }
- if (file_exists($new_path)) {
- if ($overWrite) {
- unlink($new_path);
- } else {
- throw new \Exception("new_path exits:" . $new_path);
- }
- }
- @rename($old_path, $new_path);
- return true;
- }
-
- public static function downloadFile($filepath)
- {
- if (file_exists($filepath)) {
- $filename = PathHelper::getFilename($filepath);
-
- $file1 = fopen($filepath, "r");
-
- Header("Content-type: application/octet-stream");
- Header("Accept-Ranges: bytes");
- Header("Accept-Length: " . filesize($filepath));
- Header("Content-Disposition: attachment; filename=$filename");
- echo fread($file1, filesize($filepath));
- fclose($file1);
- }
- echo "文件不存在,请联系客服处理!";
- }
-
- public static function fetchDownFile($downlink, $savePath, $saveName)
- {
- DirHelper::makeDir($savePath);
- $loaclFile = $savePath . $saveName;
- if (startWith($downlink, 'https')) {
- $user_agent = 'Mozilla/5.0 (compatible; Baiduspider/2.0; +http://www.baidu.com/search/spider.html)';
- $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';
- $randip = IpHelper::getRandIp();
- $arrContextOptions = [
- 'ssl' => [
- 'verify_peer' => false,
- 'verify_peer_name' => false,
- ],
- 'http' => [
- 'method' => "GET",
- 'header' => "User-Agent: {$user_agent}\r\nReferer: {$referer}\r\nX-FORWARDED-FOR: {$randip}\r\nCLIENT-IP: {$randip}\r\n",
- 'timeout' => 10,
- ]
- ];
-
- $response = file_get_contents($downlink, false, stream_context_create($arrContextOptions)) || die('服务器不支持采集');
- file_put_contents($loaclFile, $response);
- } else {
- ob_start();
- readfile($downlink);
- $img = ob_get_contents();
- ob_end_clean();
- file_put_contents($loaclFile, $img);
- }
- return $loaclFile;
- }
-
- public static function fetchDownFile2($downlink, $savePath, $saveName)
- {
- DirHelper::makeDir($savePath);
- $loaclFile = $savePath . $saveName;
- $curl = new CurlHelper($downlink);
- $randip = IpHelper::getRandIp();
- $curl->setHeader([
- 'CLIENT-IP:' . $randip,
- 'X-FORWARDED-FOR:' . $randip
- ]);
- $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");
- $curl->exec();
- $response = $curl->getResponse();
- if ($response && 200 == $curl->getHttpCode() && !$curl->getError()) {
- file_put_contents($loaclFile, $response);
- }
- return $loaclFile;
- }
- }
|