common.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681
  1. <?php
  2. // 应用公共文件
  3. use think\facade\Config;
  4. use think\facade\Db;
  5. use think\facade\Filesystem;
  6. use tp5er\thinkCsv\Export;
  7. // 应用公共文件
  8. function app_show($code=0,$message="",$data=[]){
  9. $result = ['code'=>$code,"message"=>$message,"data"=>$data];
  10. return json($result);
  11. }
  12. // 应用公共文件
  13. function error_show($code=0,$message=""){
  14. $result = ['code'=>$code,"message"=>$message];
  15. return json($result);
  16. }
  17. function GetUserInfo($token){
  18. $host = Config::get("app");
  19. $url = $host["api_host"]."/verifyToken";
  20. $data=[
  21. "token"=>$token
  22. ];
  23. $response=curl_request($url,$data);
  24. return json_decode($response,true);
  25. }
  26. function setUserCompany($condition){
  27. $host = Config::get("app");
  28. $url = $host["api_host"]."/setcompany";
  29. $response=curl_request($url,$condition);
  30. return json_decode($response,true);
  31. }
  32. function setCompanyStatus($condition){
  33. $host = Config::get("app");
  34. $url = $host["api_host"]."/companystatus";
  35. $response=curl_request($url,$condition);
  36. return json_decode($response,true);
  37. }
  38. function setStatus($condition){
  39. $host = Config::get("app");
  40. $url = $host["api_host"]."/userstatus";
  41. $response=curl_request($url,$condition);
  42. return json_decode($response,true);
  43. }
  44. /**手机号验证
  45. * @param $mobile
  46. * @return bool
  47. */
  48. function checkMobile($mobile){
  49. if (!is_numeric($mobile)) {
  50. return false;
  51. }
  52. return preg_match('#^1[3,4,5,6,7,8,9]{1}[\d]{9}$#', $mobile) ? true : false;
  53. }
  54. function checkTel($tel){
  55. if (!$tel) {
  56. return false;
  57. }
  58. return preg_match('/^(0[0-9]{2,3}\-)([0-9]{7,8})+(\-[0-9]{1,4})?$/', $tel) ? true : false;
  59. }
  60. /**邮箱验证
  61. * @param $email
  62. * @return bool
  63. */
  64. function checkEmail($email){
  65. if (!$email) {
  66. return false;
  67. }
  68. return preg_match('#[a-z0-9&\-_.]+@[\w\-_]+([\w\-.]+)?\.[\w\-]+#is', $email) ? true : false;
  69. }
  70. /**
  71. * @param
  72. * @return int
  73. */
  74. function makeSalt(){
  75. $salt = rand(10000000,99999999);
  76. return $salt;
  77. }
  78. /**
  79. * @param $token
  80. * @return array
  81. * @throws \think\db\exception\DataNotFoundException
  82. * @throws \think\db\exception\DbException
  83. * @throws \think\db\exception\ModelNotFoundException
  84. * @throws \think\exception\DbException
  85. */
  86. function VerifyTokens($token){
  87. $host = Config::get("app");
  88. $url = $host["api_host"]."/verifyToken";
  89. $data=[
  90. "token"=>$token
  91. ];
  92. $response=curl_request($url,$data);
  93. return json_decode($response,true);
  94. }
  95. /**
  96. * @param $token
  97. * @param $condition
  98. * @return mixed
  99. */
  100. function GetUserlist($condition){
  101. $host = Config::get("app");
  102. $url = $host["api_host"]."/userlistbycompany";
  103. $response=curl_request($url,$condition);
  104. return json_decode($response,true);
  105. }
  106. ///**
  107. // * @param $token
  108. // * @param $condition
  109. // * @return mixed
  110. // */
  111. //function GetAccountall($token, $condition){
  112. // $host = Config::get("app");
  113. //
  114. // $url = $host["api_host"]."/Api/userall";
  115. // $condition['token']=$token;
  116. // $response=curl_request($url,$condition);
  117. //
  118. // return json_decode($response,true);
  119. //}
  120. function GetList($condition){
  121. $host = Config::get("app");
  122. $url = $host["api_host"]."/userlist";
  123. $response=curl_request($url,$condition);
  124. return json_decode($response,true);
  125. }
  126. /**
  127. * @param $token
  128. * @param $condition ['id'=>1]
  129. * @return mixed
  130. */
  131. function GetInfoById($token,$condition){
  132. $host = Config::get("app");
  133. $url = $host["api_host"]."/userinfo";
  134. $condition['token']=$token;
  135. $response=curl_request($url,$condition);
  136. return json_decode($response,true);
  137. }
  138. /**
  139. * @param $str
  140. * @return string
  141. */
  142. function makeNo($str){
  143. $date=date("mdHis");
  144. $year = date("Y")-2000;
  145. $msec=rand(1000,9999);
  146. return $str.$year.$date.$msec;
  147. }
  148. function makeStr($str){
  149. $date=date("mdHis");
  150. $year = date("Y")-2000;
  151. $msec=randomkeys(4);
  152. return $str.$msec.$year.$date;
  153. }
  154. function randomkeys($length) {
  155. $returnStr='';
  156. $pattern = '1234567890abcdefghijklmnopqrstuvwxyz';//ABCDEFGHIJKLOMNOPQRSTUVWXYZ
  157. for($i = 0; $i < $length; $i ++) {
  158. $returnStr .= $pattern[mt_rand ( 0, strlen($pattern)-1 )]; //生成php随机数
  159. }
  160. return $returnStr;
  161. }
  162. /**
  163. * @param $token
  164. * @param $condition
  165. * @return mixed
  166. */
  167. function resetpwd($condition){
  168. $host = Config::get("app");
  169. $url = $host["api_host"]."/setpasswd";
  170. $response=curl_request($url,$condition);
  171. return json_decode($response,true);
  172. }
  173. function resetpasswd($token,$condition){
  174. $host = Config::get("app");
  175. $url = $host["api_host"]."/Api/passsave";
  176. $condition['token']=$token;
  177. $response=curl_request($url,$condition);
  178. return json_decode($response,true);
  179. }
  180. /**
  181. * @param $condition
  182. * @return array|bool|float|int|mixed|\stdClass|string|null
  183. */
  184. function checkLogin($condition){
  185. $host = Config::get("app");
  186. $url = $host["api_host"]."/login";
  187. $response=curl_request($url,$condition);
  188. return json_decode($response,true);
  189. }
  190. /**
  191. * @param $token
  192. * @param $condition
  193. * @return mixed
  194. */
  195. function resetinfo($condition){
  196. $host = Config::get("app");
  197. $url = $host["api_host"]."/usersave";
  198. $response=curl_request($url,$condition);
  199. return json_decode($response,true);
  200. }
  201. /**
  202. * @param $condition
  203. * @return array|bool|float|int|mixed|\stdClass|string|null
  204. */
  205. function addacount($condition){
  206. $host = Config::get("app");
  207. $url = $host["api_host"]."/useradd";
  208. $response=curl_request($url,$condition);
  209. return json_decode($response,true);
  210. }
  211. if(!function_exists("headerSign")){
  212. function headerSign($post){
  213. $config = Config::get("sign");
  214. $appid=$config['appid'];
  215. $appkey=$config['appkey'];
  216. $headerArr=["appid"=>'123',"noce"=>randomkeys(16),"sign"=>'',"timestamp"=>time()];
  217. $value =array_merge($post,$headerArr);
  218. $Sign= new \Sign($appid,$appkey);
  219. $headerArr['sign'] = $Sign->makeSign($value);
  220. $list=["Content-Type"=>"multipart/json;charset=utf-8"];
  221. foreach ($headerArr as $key=>$value){
  222. $list[]=$key.":".$value;
  223. }
  224. return $list;
  225. }
  226. }
  227. //参数1:访问的URL,参数2:post数据(不填则为GET),参数3:提交的$cookies,参数4:是否返回$cookies
  228. function curl_request($url,$post='',$header=[]){
  229. // $header = empty($header) ? '' : $header;
  230. $header = headerSign($post);
  231. if(is_array($post)) $post=http_build_query($post);
  232. $curl = curl_init();
  233. curl_setopt($curl, CURLOPT_URL, $url);
  234. curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; Trident/6.0)');
  235. curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
  236. curl_setopt($curl, CURLOPT_AUTOREFERER, 1);
  237. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
  238. if($post) {
  239. curl_setopt($curl, CURLOPT_POST, 1);
  240. curl_setopt($curl, CURLOPT_POSTFIELDS, $post);
  241. }
  242. curl_setopt($curl, CURLOPT_TIMEOUT, 10);
  243. curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  244. curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
  245. $data = curl_exec($curl);
  246. if (curl_errno($curl)) {
  247. return curl_error($curl);
  248. }
  249. curl_close($curl);
  250. return $data;
  251. }
  252. /**
  253. * @param $roleid
  254. * @param $menu
  255. * @return bool
  256. * @throws \think\db\exception\DataNotFoundException
  257. * @throws \think\db\exception\DbException
  258. * @throws \think\db\exception\ModelNotFoundException
  259. */
  260. function checkRole($roleid,$menu){
  261. $roleinfo = \think\facade\Db::name("role_action")->where([['role_id',"=",$roleid],["status","=",1]])->find();
  262. if($roleinfo['private_data']!=""){
  263. $private = explode(",",$roleinfo['private_data']);
  264. if(in_array($menu,$private)){
  265. return true;
  266. }
  267. }
  268. return false;
  269. }
  270. /**
  271. * @param $row
  272. * @param $list
  273. */
  274. function makeMenu($row,&$list){
  275. $list[$row['id']]=$row;
  276. if($row['pid']==0){
  277. return $list;
  278. }
  279. $parent =Db::name("admin_menu")->where(["id"=>$row['pid'],"status"=>1,"is_del"=>0])->field("id,menu_name,menu_img,menu_url,menu_route,pid,is_show,is_private,menu_type,weight")->find();
  280. if($parent==false) return $list;
  281. makeMenu($parent,$list);
  282. }
  283. /**
  284. * 遍历集合处理
  285. * @param $menuArr
  286. */
  287. function MenuTree(&$menuArr,$pid=0){
  288. $meun=[];
  289. foreach ($menuArr as $value){
  290. if($value['pid']==$pid){
  291. if($value['menu_type']==1)$value['child']=MenuTree($menuArr,$value['id']);
  292. $meun[]=$value ;
  293. }
  294. }
  295. return $meun;
  296. };
  297. function upload($files,$extend="xls")
  298. {
  299. // 获取表单上传文件
  300. try {
  301. validate([
  302. 'file' => [
  303. 'fileExt' => 'xlsx,xls'
  304. ]
  305. ],
  306. [
  307. 'file.fileExt' => '不支持的文件',
  308. ]
  309. )->check(['file' => $files]);
  310. if ($extend == 'xlsx') {
  311. $objReader = PHPExcel_IOFactory::createReader('Excel2007');
  312. } else {
  313. $objReader = PHPExcel_IOFactory::createReader('Excel5');
  314. }
  315. $savename = Filesystem::disk('public')->putFile('topic/excel', $files);
  316. $import_path = root_path() . 'public/storage/' . $savename;
  317. $spreadsheet = $objReader->load($import_path);
  318. $sheet = $spreadsheet->getActiveSheet();
  319. $sheetData = $sheet->toArray();
  320. if (empty($sheetData) || !is_array($sheetData)) {
  321. return ['code' => 1003, "msg" => '数据不能为空'];
  322. }
  323. return ['code' => 0, "msg" => '数据解析成功', 'data' => $sheetData];
  324. } catch (think\exception\ValidateException $e) {
  325. // echo $e->getMessage();
  326. return ['code' => 1003, "msg" => $e->getMessage()];
  327. }
  328. }
  329. /**
  330. * @param $files
  331. * @return array
  332. */
  333. function UploadImg($files)
  334. {
  335. $savename = [];
  336. $files = !is_array($files) ? [$files] : $files;
  337. try {
  338. //验证
  339. validate(['imgFile' => ['fileSize' => 10240000, 'fileExt' => 'jpg,jpeg,png,bmp,gif', 'fileMime' => 'image/jpeg,image/png,image/gif']])->check(['imgFile' => $files]);
  340. foreach ($files as $file) {
  341. $url = Filesystem::disk('public')->putFile('img/' . date("Ymd"), $file, function () use ($file) {
  342. return str_replace('.' . $file->getOriginalExtension(), '', $file->getOriginalName() . "_" . date('YmdHis'));
  343. });
  344. $name = str_replace('.' . $file->getOriginalExtension(), '', $file->getOriginalName());
  345. $temp = ["url" =>"storage/".$url, "name" => $name];
  346. $savename[] = $temp;
  347. }
  348. return $savename;
  349. } catch (\think\exception\ValidateException $e) {
  350. return $e->getMessage();
  351. }
  352. }
  353. if(!function_exists("invoiceType")){
  354. //发票类型 1 专用2普通3电子普通4 电子专用 5 q全电子发票
  355. function invoiceType($key=0){
  356. $panda= ['',"004",'007','026','028','000'];
  357. return $panda[$key]??'';
  358. }
  359. }
  360. if(!function_exists('excelExport')){
  361. /**
  362. * @param string $fileName
  363. * @param array $headArr
  364. * @param array $data
  365. * @throws \PHPExcel_Exception
  366. * @throws \PHPExcel_Reader_Exception
  367. * @throws \PHPExcel_Writer_Exception
  368. */
  369. function excelExport($fileName = '', $headArr = [], $data = [])
  370. {
  371. $objPHPExcel = new PHPExcel();
  372. $objPHPExcel->getProperties();
  373. $keyA = 0; // 设置表头
  374. foreach ($headArr as $v) {
  375. $colum = PHPExcel_Cell::stringFromColumnIndex($keyA);
  376. $objPHPExcel->setActiveSheetIndex(0)->setCellValue($colum . '1', $v);
  377. $keyA += 1;
  378. }
  379. $column = 2;
  380. $objActSheet = $objPHPExcel->getActiveSheet();
  381. foreach ($data as $key => $rows) { // 行写入
  382. $span = 0;
  383. foreach ($rows as $keyName => $value) { // 列写入
  384. //判断数据是否有数组,如果有数组,转换成字符串
  385. if(is_array($value)){
  386. $value = implode("、", $value);
  387. }
  388. $objActSheet->setCellValue(PHPExcel_Cell::stringFromColumnIndex($span) . $column, $value);
  389. $span++;
  390. }
  391. $column++;
  392. }
  393. // var_dump($objActSheet->getActiveCell());
  394. $fileName .= "_" . date("Y_m_d", time()) . ".xls";
  395. //$fileName .= "_" . date("Y_m_d", Request()->instance()->time()) . ".xls";
  396. //$fileName = iconv("utf-8", "gb2312", $fileName); // 重命名表
  397. $objPHPExcel->setActiveSheetIndex(0); // 设置活动单指数到第一个表,所以Excel打开这是第一个表
  398. // Redirect output to a client’s web browser (Excel2007)
  399. header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
  400. header('Content-Disposition: attachment;filename="'.$fileName.'"');
  401. header('Cache-Control: max-age=0');
  402. // If you're serving to IE 9, then the following may be needed
  403. header('Cache-Control: max-age=1');
  404. // If you're serving to IE over SSL, then the following may be needed
  405. header ('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
  406. header ('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT'); // always modified
  407. header ('Cache-Control: cache, must-revalidate'); // HTTP/1.1
  408. header ('Pragma: public'); // HTTP/1.0
  409. // header("Content-Type: application/octet-stream"); # 流文件输出
  410. // header("Content-Transfer-Encoding: binary"); # 告诉浏览器,这是二进制文件
  411. $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
  412. $objWriter->save('php://output'); // 文件通过浏览器下载
  413. exit();
  414. }
  415. }
  416. if(!function_exists("Csv_save")){
  417. function Csv_save($fileName,$headArr =[],$data=[],$num=10000){
  418. /*
  419. * 导出数据生成csv文件
  420. * @param file_name string 文件名
  421. * @param headArr array 表头
  422. * @param $db \think\db\Query 使用Db方法后生成的对象实例
  423. * @param callback 回调函数,一般用在对查询结果进行二次处理的时候(例如二次计算,将状态等值由数字转变为文本,额外添加字段等)
  424. */
  425. set_time_limit(0);//让程序一直运行
  426. $fileName .= date('_Y_m_d');//文件名拼接日期
  427. header('Content-Encoding:UTF-8');
  428. header("Content-type:application/vnd.ms-excel;charset=UTF-8");
  429. header('Content-Disposition:attachment;filename="' . $fileName . '.csv"');
  430. $fp = fopen('php://output', 'a');//打开php标准输出流
  431. fwrite($fp, chr(0xEF) . chr(0xBB) . chr(0xBF));//添加bom头,以UTF-8编码导出的csv文件,如果文件头未添加bom头,打开会出现乱码
  432. try {
  433. if (!empty($headArr)) fputcsv($fp, $headArr);//添加导出标题
  434. $max = PHP_INT_MAX;
  435. $count=0;//阈值
  436. //写入数据
  437. for ($i = 1; $i > 0; $i++) {
  438. $has_data = false;//默认没有查询到数据
  439. foreach ($data as $k => $value) {
  440. // $max = $value[$pk];//维护最大值
  441. // $has_data = true;//存在查询数据
  442. // if ($function) $value = $function($value);//调用回调函数处理
  443. if (empty($headArr)) {
  444. $headArr = array_keys($value);
  445. fputcsv($fp, $headArr);//添加导出标题
  446. }
  447. fputcsv($fp, $value);
  448. if (($k + 1) % $num == 0) {
  449. ob_flush();//清空缓存,释放内存
  450. flush();
  451. }
  452. $count++;//阈值自增
  453. }
  454. if ($has_data === false) break;//结束循环
  455. if($count>=1000000) throw new \think\Exception('超出文件的最大显示行数,请根据条件分批次导出数据或使用预约导出功能,更多问题请联系开发人员');
  456. }
  457. }catch (\think\Exception $exception){
  458. fputcsv($fp,[$exception->getMessage()]);
  459. }
  460. ob_flush();
  461. flush();
  462. ob_end_clean();
  463. fclose($fp);
  464. exit();
  465. }
  466. }
  467. if(!function_exists('menuAction')){
  468. function menuAction($row,&$list=[]){
  469. $temp=[];
  470. foreach ($row as $key=>$value){
  471. if($value['pid']==0){
  472. $list[]=$value;
  473. }else{
  474. $menu=Db::name("admin_menu")->where(["id"=>$value['pid'],"is_del"=>0,"status"=>1])->field("id,menu_name,menu_img,menu_route,menu_url,pid,level,is_show,is_private,menu_type,status")->findOrEmpty();
  475. if(empty($menu)) continue;
  476. if(!isset($temp[$value['pid']]))$temp[$value['pid']]=$menu;
  477. $temp[$value['pid']]['child'][]=$value;
  478. }
  479. }
  480. // $list=$temp;
  481. if (!empty($temp)){
  482. menuAction($temp,$list);
  483. }
  484. }
  485. }
  486. /**
  487. * 批量生成多个文件excel,生成压缩包,保存到本地,返回文件链接
  488. * datas 生成器
  489. * header array 头部字段
  490. * filename string 文件名
  491. */
  492. if (!function_exists('excelSaveFile')) {
  493. function excelSaveFile($datas, string $filename = '')
  494. {
  495. // $urls = [];
  496. $dir = root_path() . 'public/storage/report/' . date("YmdHis") . "/";
  497. if (!is_dir($dir)) mkdir($dir, 0777, true);
  498. // foreach ($datas as $item) {
  499. PHPExcel_Settings::setCacheStorageMethod();
  500. PHPExcel_CachedObjectStorageFactory::cache_in_memory_gzip;//单元格缓存为MemoryGZip
  501. $objPHPExcel = new PHPExcel();
  502. $keyA = 0; // 设置表头
  503. $column = 2;
  504. $objActSheet = $objPHPExcel->getActiveSheet();
  505. foreach ($datas as $key => $rows) { // 行写入
  506. //第一行取key作表头
  507. if($key==0){
  508. $objPHPExcel->getProperties();
  509. foreach ($rows as $k=>$v) {
  510. $colum = PHPExcel_Cell::stringFromColumnIndex($keyA);
  511. $objPHPExcel->setActiveSheetIndex(0)->setCellValue($colum . '1', $k);
  512. $keyA += 1;
  513. }
  514. }
  515. //写入列
  516. $span = 0;
  517. foreach ($rows as $keyName => $value) {
  518. //判断数据是否有数组,如果有数组,转换成字符串
  519. if (is_array($value)) $value = implode("、", $value);
  520. $objActSheet->setCellValue(PHPExcel_Cell::stringFromColumnIndex($span) . $column, $value);
  521. $span++;
  522. }
  523. $column++;
  524. }
  525. $file = $filename . ".xls";
  526. $objPHPExcel->setActiveSheetIndex(0);
  527. $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
  528. $objWriter->save($dir . $file); // 文件通过浏览器下载
  529. $url = $dir . $file;
  530. if (!file_exists($url)) throw new Exception('文件生成失败');
  531. $saveDir = root_path() . "public/storage/zip/" . date('Ymd') . '/';
  532. if (!is_dir($saveDir)) mkdir($saveDir, 0777, true);
  533. $file_dir = $saveDir . $filename . ".zip";
  534. # 5.1 文件打包,提示:使用本类,linux需开启zlib,windows需取消php_zip.dll前的注释
  535. $zip = new \ZipArchive ();
  536. # 5.2 文件不存在则生成一个新的文件 用CREATE打开文件会追加内容至zip
  537. if ($zip->open($file_dir, \ZipArchive::OVERWRITE) !== true && $zip->open($file_dir, \ZipArchive::CREATE) !== true) echo '无法打开文件或者文件创建失败';
  538. # 5.3 批量写入压缩包
  539. // $zip->addEmptyDir($fileName);//往zip压缩包写入空目录
  540. // foreach ($urls as $fileName) {
  541. @$zip->addFile($url, DIRECTORY_SEPARATOR . basename($url));
  542. // }
  543. // @$zip->addFile($v['file_path'], 'resume'.DIRECTORY_SEPARATOR.basename($headername));
  544. # 5.4 关闭压缩包写入
  545. $zip->close();
  546. @deldir($dir);//删除已生成的文件及目录
  547. //6. 检查文件是否存在,并输出文件
  548. if (!file_exists($file_dir)) throw new Exception('压缩包文件不存在');
  549. return str_replace(root_path()."public/" , env("host.host"), $file_dir);
  550. }
  551. }
  552. //读取大文件
  553. if (!function_exists('read_big_file')){
  554. function read_big_file(string $file=''){
  555. $handle = fopen($file, 'rb');
  556. while (feof($handle) === false) {
  557. yield fgets($handle);
  558. }
  559. fclose($handle);
  560. }
  561. }
  562. function deldir($path){
  563. //如果是目录则继续
  564. if(is_dir($path)){
  565. //扫描一个文件夹内的所有文件夹和文件并返回数组
  566. $p = scandir($path);
  567. //如果 $p 中有两个以上的元素则说明当前 $path 不为空
  568. if(count($p)>2){
  569. foreach($p as $val){
  570. //排除目录中的.和..
  571. if($val !="." && $val !=".."){
  572. //如果是目录则递归子目录,继续操作
  573. if(is_dir($path.$val)){
  574. //子目录中操作删除文件夹和文件
  575. deldir($path.$val.'/');
  576. }else{
  577. //如果是文件直接删除
  578. unlink($path.$val);
  579. }
  580. }
  581. }
  582. }
  583. }
  584. //删除目录
  585. return rmdir($path);
  586. }