php篇:随便笔记

前言

  随便笔记,随便记录

  日志输出

日志输出

file_put_contents('D:\log.txt',date('Y-m-d H:i:s')."日志内容写在这里"."\r\n\r\n" , FILE_APPEND | LOCK_EX);

 文件引入

//当前文件绝对路径 dirname(__FILE__)
require_once(dirname(__FILE__).'/inc/cfg.inc');

 zip解压

  第一种情况:php.ini 中 default_charset = "GBK" ;操作系统 window

            $zip = new \ZipArchive();
            if ($zip->open(mb_convert_encoding($fileName, 'UTF-8', 'GBK'))=== true) {
                //提前遍历一边文件,防止有除了html,htm的其他文件存在,并且统计文件数量。
                $file_num = 0;
                $continue_mark = false;
                for ($j = 0;$j<$zip->numFiles;$j++){
                    $statInfo = $zip->statIndex($j, ZipArchive::FL_ENC_RAW);
                    $sfx = trim(strrchr($statInfo['name'],'.'),'.');
                    if ($statInfo['name'][strlen($statInfo['name'])-1] === '/')
                        continue;
                    if (!($sfx === 'html' || $sfx === 'htm')){
                        $log['n_fail'] = '1';
                        $log['fail_info'] .= $arch_name.' 压缩包里存在'.$statInfo['name'].'文件,只支持html、htm和文件夹;';//使用$log['fail_info']需要";"结尾
                        add_log($arch_name,$file_type);
                        $zip->close();//关闭文件句柄
                        unlink($fileName);
                        $continue_mark = true;
                        break;
                    }
                    $file_num++;
                }
                if ($continue_mark){//说明压缩包里有除了html、htm之外的文件,跳过该次文件处理
                    continue;
                }
                $log['n_html'] = $file_num;//压缩包里html总的数量
                $res = zip_open(mb_convert_encoding($fileName, 'UTF-8', 'GBK'));
                $index = 0;
                while( $fr = zip_read($res) ){
                    //压缩包解压api
                    $statInfo = $zip->statIndex($index, ZipArchive::FL_ENC_RAW);
                    if ($statInfo['name'][strlen($statInfo['name'])-1] === '/'){//判断如果是文件夹
                        ensure_dir_writable($workspace_path.$preName.'/'.$statInfo['name']);
                        $index++;
                        continue;
                    }
                    ensure_dir_writable($run_path.'/'.substr($statInfo['name'],0,strrpos($statInfo['name'],'/')));
                    $content = zip_entry_read($fr,zip_entry_filesize($fr));
                    file_put_contents($run_path.'/'.$statInfo['name'], $content);
                    $index++;
                }
                if (file_exists($run_path)){//判断文件解压是否存在
                    praseHtmlFileZip($run_path);//解析字段方法
                    delAll($run_path);//删除解压的文件
                }else{
                    $log['n_fail'] = '1';
                    $log['fail_info'] .= $archName.' 压缩包解压失败;';//使用$log['fail_info']需要";"结尾
                    add_log($archName,$file_type);
                }
                add_log($archName,$file_type,true);//一份zip文件处理完成,重置log变量
                $zip->close();
                unset($res);
            }else{
                $log['n_fail'] = '1';
                $log['fail_info'] .= $archName.' 压缩包读取失败;';//使用$log['fail_info']需要";"结尾
                add_log($archName,$file_type);
            }

   第二种情况:php.ini 中 default_charset = "UTF-8" ;操作系统 window   

      $zip = new \ZipArchive();
      if ($res = $zip->open($fileName)){
        $fileNum = $zip->numFiles;
        for ($i = 0; $i < $fileNum; $i++) {
            $statInfo = $zip->statIndex($i, ZipArchive::FL_ENC_RAW);
            $zip->renameIndex($i, iconv('GBK', 'utf-8//IGNORE', $statInfo['name']));  //在执行extracTo函数前,先将中文文件名转码,防止乱码
        }
        $zip->close();
        $zip->open($fileName);
         $zip->extractTo($path);
      }

隐藏响应中apache、php的服务器相关信息

apache 中 http.conf  ( server中的apache信息)

ServerSignature Off
ServerTokens Prod

php 中 php.ini(X-Powered-By中的php信息)

expose_php = Off
原文地址:https://www.cnblogs.com/shuzhixia/p/15748001.html