程式CODE
2016年10月5日 星期三
2015年7月28日 星期二
cache語法!搭配序列化及反序列
<?php
/*
這是一個簡易的快取物件cache
作者:二林網管 村仔 2009.09.07
公共GPL版權
用法:
$Cache=new myCache();
$Cache->dir='/home/edu/html';
$Cache->type='text';//預設為陣列,非陣列才寫
$Cache->save('名稱',$data);
$Cache->get('名稱');//取資料
*/
class myCache{
private $ext='.cache';//附檔名
var $dir;//快取檔案路徑
public $limit=300;//5分*60秒 多少時間內的線上人數,可由外部指定
public $type='array';//或text
//建構函式,起始
public function __construct() {
}
//存入快取
public function save($id,$data) {
$fn=$this->dir.$id.$this->ext;
//先判斷檔案存在否
if (file_exists($fn)) {$this->del($id);}//存在時如何處理
if ($this->makeFile($fn)){
if ($this->type=='array'){$str=serialize($data);}else{$str=$data;}
$fp=fopen($fn,"w");
fwrite ($fp,$str);
fclose($fp);
}
}
//取出快取
public function get($id) {
$fn=$this->dir.$id.$this->ext;
//先判斷檔案存在否
if (!file_exists($fn)) {return false;}//存在時如何處理
$str = @file_get_contents($fn);
if (!empty($str)){
if ($this->type=='array'){$data=unserialize($str);}else{$data=$str;}
//$data=unserialize($str);
return $data;
}
}
//刪除快取
public function del($id) {
$fn=$this->dir.$id.$this->ext;
if (file_exists($fn)) unlink($fn);//存在時如何處理
}
public function makeFile($file) {
$fp=fopen($file,"a");
if ($fp){fclose($fp);return true;}
else {return false;}
}
}
//end class
2013年12月2日 星期一
列出php.ini的設定
ini_get(memory_limit)
ini_get(post_max_size)
ini_get(upload_max_filesize)
有個ini_set()可以設定
但並不是每一個設定值都可以用php改
ini_get(post_max_size)
ini_get(upload_max_filesize)
有個ini_set()可以設定
但並不是每一個設定值都可以用php改
2013年12月1日 星期日
2013年11月21日 星期四
php 列出資料夾內的檔案和目錄
if ($handle = opendir('.')) { //開啟現在的資料夾
while (false !== ($file = readdir($handle))) {
//避免搜尋到的資料夾名稱是false,像是0
if ($file != "." && $file != "..") {
//去除掉..跟.
echo "<a href='$file'>$file</a><br>";
}
}
closedir($handle);
}
foreach(glob('*',GLOB_ONLYDIR) as $directory)
{
echo 'Directory name: ' . $directory . '<br />';
}
while (false !== ($file = readdir($handle))) {
//避免搜尋到的資料夾名稱是false,像是0
if ($file != "." && $file != "..") {
//去除掉..跟.
echo "<a href='$file'>$file</a><br>";
}
}
closedir($handle);
}
foreach(glob('*',GLOB_ONLYDIR) as $directory)
{
echo 'Directory name: ' . $directory . '<br />';
}
2013年10月24日 星期四
php解壓檔案
function unzip(){
global $UPLOAD_PATH;
$is_win=ereg('win', strtolower($_SERVER['SERVER_SOFTWARE']))?true:false;
$zipfile=($is_win)?"UNZIP32.EXE":"/usr/bin/unzip";
$arg1=($is_win)?"START /min cmd /c ":"";
$arg2=($is_win)?"-d":"-d";
if($_FILES['file']['type'] == "application/vnd.sun.xml.writer"){
$filename="sample.sxw";
}elseif(strtolower(substr($_FILES['file']['name'],-3))=="sxw"){
$filename="sample.sxw";
}else{
die("格式不正確");
}
if (!is_dir($UPLOAD_PATH)) {
die("上傳目錄 $UPLOAD_PATH 不存在!");
}
//統一上傳目錄
$upath=$UPLOAD_PATH."time_table";
if (!is_dir($upath)) {
mkdir($upath) or die($upath."建立失敗!");
}
//上傳目的地
$todir=$upath."/sample/";
if (is_dir($todir)) {
//刪除目錄所有檔案
deldir($todir);
mkdir($todir) or die($todir."目的目錄建立失敗!");
}else{
mkdir($todir) or die($todir."目的目錄建立失敗!");
}
$the_file=$todir.$filename;
copy($_FILES['file']['tmp_name'],$the_file);
unlink($_FILES['file']['tmp_name']);
if (!file_exists($zipfile)) {
die($zipfile."不存在!");
}elseif(!file_exists($the_file)) {
die($the_file."不存在!");
}
$cmd=$arg1." ".$zipfile." ".$the_file." ".$arg2." ".$todir;
if(exec($cmd,$output,$rv)){
return;
}else{
$msg=$cmd."已執行。<br>";
foreach($output as $v){
$msg.=$v."<br>";
}
return $msg;
}
}
2013年10月23日 星期三
讀取CSV檔
//從csv檔取名單
$fp = fopen(檔案位置, "r");
while ( $ROW = __fgetcsv($fp) ) {
$ROW[0];//欄位1
$ROW[1];//欄位2
.
.
.
}
原生fgetcsv讀取中文函式亂碼問題修正
/**
* fgetcsv
*
* 修正原生fgetcsv讀取中文函式
*
* @param CSV文件檔案
* @param length 每一行所讀取的最大資料長度
* @param d 資料分隔符號(預設為逗號)
* @param e 字串包含符號(預設為雙引號)
* @return $_csv_data
*/
function __fgetcsv(&$handle, $length = null, $d = ",", $e = '"') {
$d = preg_quote($d);
$e = preg_quote($e);
$_line = "";
$eof=false;
while ($eof != true) {
$_line .= (empty ($length) ? fgets($handle) : fgets($handle, $length));
$itemcnt = preg_match_all('/' . $e . '/', $_line, $dummy);
if ($itemcnt % 2 == 0){
$eof = true;
}
}
$_csv_line = preg_replace('/(?: |[ ])?$/', $d, trim($_line));
$_csv_pattern = '/(' . $e . '[^' . $e . ']*(?:' . $e . $e . '[^' . $e . ']*)*' . $e . '|[^' . $d . ']*)' . $d . '/';
preg_match_all($_csv_pattern, $_csv_line, $_csv_matches);
$_csv_data = $_csv_matches[1];
for ($_csv_i = 0; $_csv_i < count($_csv_data); $_csv_i++) {
$_csv_data[$_csv_i] = preg_replace("/^" . $e . "(.*)" . $e . "$/s", "$1", $_csv_data[$_csv_i]);
$_csv_data[$_csv_i] = str_replace($e . $e, $e, $_csv_data[$_csv_i]);
}
return empty ($_line) ? false : $_csv_data;
}
參考自:
http://blog.csdn.net/shilian_h/article/details/4371051
php上傳檔案相關程式
使用php進行檔案上傳
上傳的目錄權限必須777
接收端程式
其他相關函式
file_exists(檔案位置):檔案是否存在
unlink(檔案位置):刪除檔案
//刪除目錄
上傳的目錄權限必須777
<form action=db.php method='post' ENCTYPE='multipart/form-data'>
<input type=file name=file>
<input type=submit value=送出>
</form>
接收端程式
move_uploaded_file($_FILES["file"]["tmp_name"],檔案位置);
其他相關函式
file_exists(檔案位置):檔案是否存在
unlink(檔案位置):刪除檔案
//刪除目錄
function deldir($dir){
$current_dir = opendir($dir);
while($entryname = readdir($current_dir)){
if(is_dir("$dir/$entryname") and ($entryname != "." and $entryname!="..")){
deldir("${dir}/${entryname}");
}elseif($entryname != "." and $entryname!=".."){
unlink("${dir}/${entryname}");
}
}
closedir($current_dir);
rmdir(${dir});
}
2013年9月30日 星期一
FCKeditor2.6.6在php
我覺得FCKeditor還是比CKeditor或tinymce好用
光可以直接上傳圖片就贏了,不用再裝東裝西搞不定
一、開啟上傳功能
# vim fckeditor/editor/filemanager/connectors/php/config.php
改為true
$Config['Enabled'] = true ;
改上傳路徑,該路徑要設777
$Config['UserFilesPath'] = '/UploadFCK/' ;
二、php檔(在FCKeditor目錄外)引入FCKeditor
$oFCKeditor = new FCKeditor('FCKeditor1') ;
$oFCKeditor->BasePath = './fckeditor/' ;
$oFCKeditor->Value = '這裡是預設輸入的內容' ;
$oFCKeditor->Create() ;
三、處理資料,$postedValue即是輸入內容
if ( isset( $_POST ) )
$postArray = &$_POST ; // 4.1.0 or later, use $_POST
else
$postArray = &$HTTP_POST_VARS ; // prior to 4.1.0, use HTTP_POST_VARS
foreach ( $postArray as $sForm => $value )
{
if ( get_magic_quotes_gpc() )
$postedValue = htmlspecialchars( stripslashes( $value ) ) ;
else
$postedValue = htmlspecialchars( $value ) ;
echo $postedValue;
};
2013年9月29日 星期日
中文大寫數字圖形驗證碼
記得 php 的 gd2模組要裝
原理:
(1)一個pic.php檔做圖形認證,把它當成一個圖檔
(2)一個登入畫面檔index.php提供使用者輸入驗證碼$key,並引入圖形
(3)再一個php去比對使用者輸入的$key和$_SESSION['key']是否一樣
參考自:
http://tad0616.net/modules/tad_book3/page.php?tbdsn=219
http://php.freehostingguru.com/function.php-ImageTTFText.php
原理:
(1)一個pic.php檔做圖形認證,把它當成一個圖檔
session_start();
//隨機取5碼key
$key = rand(10000,99999);
//將key存入session
$_SESSION['key'] = $key;
//阿拉伯數字轉中文大寫
$cht = array(0=>"零",1=>"壹",2=>"貳",3=>"參",4=>"肆",5=>"伍",6=>"陸",7=>"柒",8=>"捌",9=>"玖");
for($i=0;$i<5;$i++) $cht_key.=$cht[substr($key,$i,1)];
header("Content-type: image/gif");
$im = imagecreatefromgif("./images/captcha_bk.gif") or die("無法建立GD圖片");
$text_color = imagecolorallocate($im, 160, 90, 44);
//選定中文字型
imagettftext($im, 50, 0 , 61, 49, $text_color,"./font/wt071.ttf",$cht_key);
imagegif($im);
imagedestroy($im);
(2)一個登入畫面檔index.php提供使用者輸入驗證碼$key,並引入圖形
<img src=pic.php>
(3)再一個php去比對使用者輸入的$key和$_SESSION['key']是否一樣
if($_SESSION['key']!=$key or empty($_SESSION['key'])){
return "驗證碼不符";
}else{
return "驗證成功!";
}
參考自:
http://tad0616.net/modules/tad_book3/page.php?tbdsn=219
http://php.freehostingguru.com/function.php-ImageTTFText.php
2013年4月7日 星期日
PHP5函數備忘
PHP5的常用函數備忘
一、字串
(1) substr(字串,0,10);
取部分字串,從頭開始,取10個字元
(2)trim(字串);
頭尾去空白
(3) explode(",", 字串);
用「,」切割字串...成為一個陣列,再用count(陣列)數有幾個
(4)nl2br(字串);
把字串裡的換行符號\n,轉為<br>
(5)str_replace(被取代,變為,字串)
把字串裡要被取代的字,變為想要的另一組字
(6)sprintf("%03s",變數)
$stud_tel_3 = sprintf("%-14s",$stud_tel_3);
表示變數的字串要14位元,補空白,補在左邊
$stud_tel_3 = sprintf("%014s",$stud_tel_3);
表示變數的字串要14位元,補0,補在右邊
(7)strtolower()
大寫英文轉小寫
二、數字
(1)round($n);
四捨五入
(2)ceil($n);
無條件進位
(3)floor($n);
無條件捨去
三、時間
(1)date("YmdHis");
例如:201203041530
(2)date('Y-m-d');
例如:2012-03-04
四、網路
(1)$_SERVER["REMOTE_ADDR"];
取使用者ip
(2) header("Location: index.php");
轉頁面到index.php
(3)$_REQUEST['變數名']
同時可取到post或get傳來的值(一招取代$_POST和$_GET)
(4)move_uploaded_file($_FILES["file"]["tmp_name"][$i],儲存路徑);
儲存上傳檔案
(5)$_FILES["file"]["size"][$i],$_FILES["file"]["name"][$i];
上傳檔案的容量,檔名
五、判別
(1)is_numeric(變數)
判斷變數是否為數字或數字的字串
一、字串
(1) substr(字串,0,10);
取部分字串,從頭開始,取10個字元
(2)trim(字串);
頭尾去空白
(3) explode(",", 字串);
用「,」切割字串...成為一個陣列,再用count(陣列)數有幾個
(4)nl2br(字串);
把字串裡的換行符號\n,轉為<br>
(5)str_replace(被取代,變為,字串)
把字串裡要被取代的字,變為想要的另一組字
(6)sprintf("%03s",變數)
$stud_tel_3 = sprintf("%-14s",$stud_tel_3);
表示變數的字串要14位元,補空白,補在左邊
$stud_tel_3 = sprintf("%014s",$stud_tel_3);
表示變數的字串要14位元,補0,補在右邊
(7)strtolower()
大寫英文轉小寫
二、數字
(1)round($n);
四捨五入
(2)ceil($n);
無條件進位
(3)floor($n);
無條件捨去
三、時間
(1)date("YmdHis");
例如:201203041530
(2)date('Y-m-d');
例如:2012-03-04
四、網路
(1)$_SERVER["REMOTE_ADDR"];
取使用者ip
(2) header("Location: index.php");
轉頁面到index.php
(3)$_REQUEST['變數名']
同時可取到post或get傳來的值(一招取代$_POST和$_GET)
(4)move_uploaded_file($_FILES["file"]["tmp_name"][$i],儲存路徑);
儲存上傳檔案
(5)$_FILES["file"]["size"][$i],$_FILES["file"]["name"][$i];
上傳檔案的容量,檔名
五、判別
(1)is_numeric(變數)
判斷變數是否為數字或數字的字串
訂閱:
文章 (Atom)