<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                [TOC] ## 一鍵清空緩存 通用清除緩存函數申明,調用不詳述。具體參考我學習筆記: http://www.hmoore.net/wayanbao/thinkphp/811941 ### 1、清空數據緩存 ``` /** * 清空數據緩存 不刪出cache目錄 * 2018.10.22 By wyb */ function clear_file_cache($path) { $dh = opendir($path); while (($file = readdir($dh)) !== false ) { if ($file != "." && $file != "..") { $fullpath = $path . $file; if (!is_dir($fullpath)) { array_map( 'unlink', glob($fullpath)); //echo '【刪除文件】'.$fullpath ; } else { //echo '【文件夾】'.$fullpath ; $newpath = $fullpath . DS; clear_file_cache($newpath); @rmdir($newpath); } } } closedir($dh); return true; //刪除當前文件夾: //if(rmdir($path)) { // return true; //} else { // return false; //} } ``` ### 2、清空模板緩存 ``` /** * 清除模板緩存 不刪除 temp目錄 * 2018.10.22 By wyb */ function clear_tmp_cache($path) { if(array_map('unlink', glob( $path.'*.php' ))){ return true; } else { return false; } } ``` ### 3、清空日志緩存 ``` /** * 清除日志緩存 不刪出log目錄 * 2018.10.22 By wyb */ function clear_log_cache($path) { $dh = opendir($path); while (($file = readdir($dh)) !== false ) { if ($file != "." && $file != "..") { $fullpath = $path . $file; if (!is_dir($fullpath)) { array_map( 'unlink', glob($fullpath)); //echo '【刪除文件】'.$fullpath ; } else { //echo '【文件夾】'.$fullpath ; $newpath = $fullpath . DS; clear_log_cache($newpath); @rmdir($newpath); } } } closedir($dh); return true; //刪除當前文件夾: //if(rmdir($path)) { // return true; //} else { // return false; //} } ``` ### 4、一鍵清空緩存 ``` /** * 一鍵清空數據緩存cache、模板緩存tmp,不刪除 cache、tmp相關目錄 * 2018.10.22 By wyb */ public function clearAll() { // 首先,清空模板緩存tmp $path = TEMP_PATH; if(!@clear_tmp_cache($path)){ return error('清除模板緩存失敗'); } // 其次,清空數據緩存cache $c_path = CACHE_PATH; if(!@clear_file_cache($c_path)){ return error('清空文件緩存失敗'); } // 最后,信息提示 return success('一鍵清空緩存成功'); } ``` ## 自定義助手函數步驟 ### 1、新建common控制器(build文件創建) ~~~ return [ // 生成應用公共文件 '__file__' => ['common.php', 'config.php', 'database.php'], // 定義demo模塊的自動生成 (按照實際定義的文件名生成) 'admin' => [ '__file__' => ['common.php'], '__dir__' => ['behavior', 'controller', 'model', 'view'], 'controller' => ['Index','User','Config','Category','Menu','Common'], 'model' => ['Models', 'ModelsField','Config','Category','Menu'], 'view' => ['index/index','config/index','category/index','menu/index'], ], // 其他更多的模塊定義 ]; ~~~ ### 2、繼承common控制器 ~~~ <?php namespace app\admin\controller; use think\Controller; use think\Db; use util\Tree; class Common extends Controller { …… } ~~~ ### 3、編寫公共函數,如刪除 ~~~ <?php namespace app\admin\controller; use think\Controller; use think\Db; use util\Tree; class Common extends Controller { // 刪除分類 public function delete($id = 0,$tab = 1){ // 獲取控制器(當前表名) $table = request()->controller(); if (Db::name($table)->where('id',$id)->delete()) { return success('分類成功',url('index',['tab'=>$tab])); }else{ $this->error('分類失敗',url('index',['tab'=>$tab])); } } } ~~~ ## 常用公共函數 ### 自定義success助手函數 ~~~ function success($msg = '成功', $url = '') { $data['status'] = 200; $data['msg'] = $msg; $data['url'] = $url; return json($data); } ~~~ ### 自定義error助手函數 ~~~ function error($msg = '失敗', $url = '') { $data['status'] = 202; $data['msg'] = $msg; $data['url'] = $url; return json($data); } ~~~ ### 檢測數據表是否存在 ~~~ /** * [table_exists 檢測數據表是否存在] * @param string $tablename [表名,不含前綴] * @return [bool] [存在返回true,不存在返回false] */ function table_exists($tablename='') { //獲取所有數據表名 $tables = []; $data = Db::query('SHOW TABLES'); foreach ($data as $value) { $tables[] = $value['Tables_in_'.config('database.database')]; } //獲取表前綴 $dbPrefix = config('database.prefix'); //當前的表名 $tablename=$dbPrefix.$tablename; if(in_array($tablename,$tables)){ return true; //存在 }else { return false; //不存在 } } ~~~ ### 檢測數據表字段是否存在 ~~~ /** * [field_exists 檢測數據表字段是否存在] * @param string $tablename [表名,不含前綴] * @param string $field [字段名] * @return [bool] [存在返回true,不存在返回false] */ function field_exists($tablename='', $field='') { //獲取表前綴 $dbPrefix = config('database.prefix'); //先判斷數據表是否存在 if(table_exists($tablename)){ $fieldArray = Db::query("Describe `{$dbPrefix}{$tablename}` `{$field}` ;"); if(is_array($fieldArray[0])){ return true; //存在 }else { return false; //不存在 } }else { return false; //不存在 } } ~~~ ### 根據分類ID獲取相應分類信息 ~~~ /** * @foo 根據分類ID獲取相應分類信息 * @Author w.y.b * @DateTime 2018-07-21 * @param integer $id [分類ID] * @param string $field [分類字段] * @return [type] [有第二個字段參數,則獲取某個字段;否則獲取單條分類數據] */ function getCatInfoById($id=0, $field=''){ if($field == ''){ //獲取單條數據 return Db::name('category')->where('id',$id)->find(); }else{ //獲取某個字段 return Db::name('category')->where('id',$id)->value($field); } } ~~~ ### 根據分類ID獲取相應模型信息 ~~~ /** * @foo 根據分類ID獲取相應模型信息 * @Author w.y.b * @DateTime 2018-07-21 * @param integer $id [分類ID] * @param string $field [模型字段] * @return [type] [有第二個模型字段參數,則獲取某個字段;否則獲取單條模型數據] */ function getModInfoById($id=0, $field=''){ //模型ID、 $modelId = getCatInfoById($id, 'modelid'); if($field == ''){ //獲取單條數據 return Db::name('models')->where('id',$modelId)->find(); }else{ //獲取某個字段 return Db::name('models')->where('id',$modelId)->value($field); } } ~~~ ### PHP異位或加密實現自動登陸 ``` /** * 異位或加密字符串 * @param [String] $value [需要加密的字符串] * @param [integer] $type [加密解密(0:加密,1:解密)] * @return [String] [加密或解密后的字符串] */ function encryption ($value, $type=0) { $key = md5(C('ENCTYPTION_KEY')); if (!$type) { return str_replace('=', '', base64_encode($value ^ $key)); } $value = base64_decode($value); return $value ^ $key; } ``` 詳情見: [PHP異位或加密實現自動登陸](http://www.hmoore.net/book/wayanbao/thinkphp/preview/PHP%E5%BC%82%E4%BD%8D%E6%88%96%E5%8A%A0%E5%AF%86%E5%AE%9E%E7%8E%B0%E8%87%AA%E5%8A%A8%E7%99%BB%E9%99%86.md) [自動登錄完成](http://www.hmoore.net/book/wayanbao/thinkphp/preview/%E8%87%AA%E5%8A%A8%E7%99%BB%E5%BD%95%E5%AE%8C%E6%88%90.md) ### 退出登錄處理 ``` /** * 退出登錄處理 */ Public function loginOut () { //卸載SESSION session_unset(); session_destroy(); //刪除用于自動登錄的COOKIE @setcookie('auto', '', time() - 3600, '/'); //跳轉致登錄頁 redirect(U('Login/index')); } ``` ### 圖片上傳處理 首先,函數申明 ``` /** * 圖片上傳處理 * @param [String] $path [保存文件夾名稱] * @param [String] $width [縮略圖寬度多個用,號分隔] * @param [String] $height [縮略圖高度多個用,號分隔(要與寬度一一對應)] * @return [Array] [圖片上傳信息] */ Private function _upload ($path, $width, $height) { import('ORG.Net.UploadFile'); //引入ThinkPHP文件上傳類 $obj = new UploadFile(); //實例化上傳類 $obj->maxSize = C('UPLOAD_MAX_SIZE'); //圖片最大上傳大小 $obj->savePath = C('UPLOAD_PATH') . $path . '/'; //圖片保存路徑 $obj->saveRule = 'uniqid'; //保存文件名 $obj->uploadReplace = true; //覆蓋同名文件 $obj->allowExts = C('UPLOAD_EXTS'); //允許上傳文件的后綴名 $obj->thumb = true; //生成縮略圖 $obj->thumbMaxWidth = $width; //縮略圖寬度 $obj->thumbMaxHeight = $height; //縮略圖高度 $obj->thumbPrefix = 'max_,medium_,mini_'; //縮略圖后綴名 $obj->thumbPath = $obj->savePath . date('Y_m') . '/'; //縮略圖保存圖徑 $obj->thumbRemoveOrigin = true; //刪除原圖 $obj->autoSub = true; //使用子目錄保存文件 $obj->subType = 'date'; //使用日期為子目錄名稱 $obj->dateFormat = 'Y_m'; //使用 年_月 形式 if (!$obj->upload()) { return array('status' => 0, 'msg' => $obj->getErrorMsg()); } else { $info = $obj->getUploadFileInfo(); $pic = explode('/', $info[0]['savename']); return array( 'status' => 1, 'path' => array( 'max' => $pic[0] . '/max_' . $pic[1], 'medium' => $pic[0] . '/medium_' . $pic[1], 'mini' => $pic[0] . '/mini_' . $pic[1] ) ); } } ``` 其次,函數調用 ``` /** * 頭像上傳 */ Public function uploadFace () { if (!$this->isPost()) { halt('頁面不存在'); } $upload = $this->_upload('Face', '180,80,50', '180,80,50'); echo json_encode($upload); } ``` 第三,配置文件 ``` <?php return array( //圖片上傳 'UPLOAD_MAX_SIZE' => 2000000, //最大上傳大小 'UPLOAD_PATH' => './Uploads/', //文件上傳保存路徑 'UPLOAD_EXTS' => array('jpg', 'jpeg', 'gif', 'png'), //允許上傳文件的后綴 ); ?> ``` 第四,修改頭像 ``` /** * 修改用戶頭像(后臺處理) */ Public function editFace () { if (!$this->isPost()) { halt('頁面不存在'); } $db = M('userinfo'); $where = array('uid' => session('uid')); $field = array('face50', 'face80', 'face180'); $old = $db->where($where)->field($field)->find(); if ($db->where($where)->save($_POST)) { if (!empty($old['face180'])) { @unlink('./Uploads/Face/' . $old['face180']); @unlink('./Uploads/Face/' . $old['face80']); @unlink('./Uploads/Face/' . $old['face50']); } $this->success('修改成功', U('index')); } else { $this->error('修改失敗,請重試...'); } } ``` ### 格式化時間(評論與微博) ``` /** * 格式化時間 * @param [type] $time [要格式化的時間戳] * @return [type] [description] */ function time_format ($time) { //當前時間 $now = time(); //今天零時零分零秒 $today = strtotime(date('y-m-d', $now)); //傳遞時間與當前時秒相差的秒數 $diff = $now - $time; $str = ''; switch ($time) { case $diff < 60 : $str = $diff . '秒前'; break; case $diff < 3600 : $str = floor($diff / 60) . '分鐘前'; break; case $diff < (3600 * 8) : $str = floor($diff / 3600) . '小時前'; break; case $time > $today : $str = '今天&nbsp;&nbsp;' . date('H:i', $time); break; default : $str = date('Y-m-d H:i:s', $time); } return $str; } ```
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看