////////////////////////////////////////////////////////////
//$dir要刪除的目錄,$DeleteMe是否連目錄本身也要刪掉
/*
$log = '/home/logs/2011';
SureRemoveDir($log , true); // 第二個參數: true 連 2011 目錄也刪除
*/
////////////////////////////////////////////////////////////
function SureRemoveDir($dir, $DeleteMe)
{
if(!$dh = @opendir($dir))
return;
while(false !== ($obj = readdir($dh)))
{
if($obj=='.' || $obj=='..')
continue;
$temp = $dir.$obj;
echo "刪除 ".$temp."
";
$conut++;
if(!@unlink($dir.'/'.$obj))
SureRemoveDir($dir.'/'.$obj, true);
}
echo "已刪除".$conut."個檔案!";
if ($DeleteMe)
{
closedir($dh);
@rmdir($dir);
echo "已刪除目錄!";
}
}
2012年7月31日 星期二
PHP Delete all files in folder
一次清空目錄下所有檔案
2012年7月30日 星期一
PHP 去除字串中特殊字元或符號
有時需要對字串作特殊處理,像是在處理檔名時去要去除特殊符號。
提供一簡單的function來方處理。
提供一簡單的function來方處理。
////////////////////////////////////////////////////////////
//$targetString要處理的字串,要移除的符號陣列$symbolArray
//使用範例
/*
$temp = "adn?cklmc?=kkal;m!c";
$sArray = array("?", "!", "=");
replaceSepcialSymbol($temp, $sArray);
echo $temp;
*/
////////////////////////////////////////////////////////////
function replaceSepcialSymbol(&$targetString, $symbolArray)
{
foreach ($symbolArray as $value)
$targetString = str_replace($value, "", $targetString);
}
PHP GET URL截斷問題處理
範例
http://api.taipeibus.kasper.com.tw/?url=http://imp.5284.com.tw:3621/TaipeiBusService/Route.aspx?Column=providerId,1100&DataFormat=json
原本期待可以使用$_GET["url"]取得
http://imp.5284.com.tw:3621/TaipeiBusService/Route.aspx?Column=providerId,1100&DataFormat=json
但其實只能得到
http://imp.5284.com.tw:3621/TaipeiBusService/Route.aspx?Column=providerId,1100
在"&"之後會被截斷
可以改用
http://api.taipeibus.kasper.com.tw/?url=http://imp.5284.com.tw:3621/TaipeiBusService/Route.aspx?Column=providerId,1100&DataFormat=json
原本期待可以使用$_GET["url"]取得
http://imp.5284.com.tw:3621/TaipeiBusService/Route.aspx?Column=providerId,1100&DataFormat=json
但其實只能得到
http://imp.5284.com.tw:3621/TaipeiBusService/Route.aspx?Column=providerId,1100
在"&"之後會被截斷
可以改用
$query_url = str_replace("url=", "", $_SERVER['QUERY_STRING'];
2012年7月27日 星期五
PHP echo 中文亂碼處理
用php直接echo "中文"; 在瀏覽器上會看到亂碼,在開頭加入header即可解決。
<?php
header("Content-Type:text/html; charset=utf-8");
?>
2012年7月26日 星期四
PHP cache demo
1.收到要求
2.檢查要求檔案是否過期
3.重新要新的或將cache給client
2.檢查要求檔案是否過期
3.重新要新的或將cache給client
<?php
////////////////////////////
//將從http(或其他)讀來的檔案
//使用範例
// http://api.taipeibus.kasper.com.tw/?url=http://tw.yahoo.com/
// http://api.taipeibus.kasper.com.tw/?url=http://imp.5284.com.tw:3621/TaipeiBusService/Route.aspx?DataFormat=json
////////////////////////////
function outputHttpResult($url)//您想抓取的網址
{
//檔案名稱
$filename = "./cache/".str_replace("http://imp.5284.com.tw:3621/TaipeiBusService/", "", $url).".html";
//檢查檔案時間
$filetime = filemtime($filename);
if($filetime == 0)
{
//無此檔案 要新的
getApiResultAndSave($url);
}
else
{
//檔案是否過期
if((mktime() - $filetime) >= 30)
{
//超過30秒 要新的
getApiResultAndSave($url);
}
else
{
//還沒超過30秒 給cache的檔案
readCacheFileAndEcho($filename);
}
}
}
//取得http資源存檔並回給client
function getApiResultAndSave($url)
{
//檔案名稱
$filename = "./cache/".str_replace("http://imp.5284.com.tw:3621/TaipeiBusService/", "", $url).".html";
$buffer = file($url); //將網址讀入buffer變數
for($i = 0; $i < sizeof($buffer); $i++) //將每段文字讀出來,以換行為單位,sizeof會傳回共有幾筆
{
echo $buffer[$i]; //全部印出來
}
// 要寫入的內容
for($i = 0; $i < sizeof($buffer); $i++) //將每段文字讀出來,以換行為單位,sizeof會傳回共有幾筆
{
$word .= $buffer[$i]; //全部接起來
}
//$word = $buffer;
$fh = fopen($filename, "w");
fwrite($fh, $word);
fclose($fh);
}
//讀出舊的檔案並回給client
function readCacheFileAndEcho($filename)
{
$fh = @fopen($filename,"r") or die("READ FILE ERROR!");
if($fh)
{
while(!feof($fh))
{
echo fgets($fh);
}
}
fclose($fh);
}
?>
2012年7月24日 星期二
PHP include html demo
this is used to manager website's main menu.
or some repeated html code.
Here is an example.
share_main_menu.html for reusing.
when you went to use...
or some repeated html code.
Here is an example.
share_main_menu.html for reusing.
<a href="admin_news.php">管理最新消息</a> | <a href="admin_case.php">管理成功案例</a> | <a href="admin_contact.php">管理聯絡我們</a> | <a href="admin_quest.php">管理案件</a> | <a href="admin_reserve.php">管理備料</a> | <a href="?logout=true">登出系統</a> <hr size="1" />
when you went to use...
2012年7月23日 星期一
PHP get monthly serial number
PHP版本的自動生成有規則的訂單號(或編號)
生成的格式是:200908010001 前面幾位為當前的日期,後面五位為系統自增長類型的編號原理:
1.獲取當前日期格式化值
2.讀取文件,上次編號的值+1最為當前此次編號的值(記錄以文件的形式存儲,下月會接著這個編號)
注意:網站目錄下需有EveryDaySerialNumber.dat否則會發生錯誤
使用範例(一)
使用範例(二)
生成的格式是:200908010001 前面幾位為當前的日期,後面五位為系統自增長類型的編號原理:
1.獲取當前日期格式化值
2.讀取文件,上次編號的值+1最為當前此次編號的值(記錄以文件的形式存儲,下月會接著這個編號)
<?php
class FileEveryDaySerialNumber
{
private $filename; //檔名
private $separate; //系統分隔符號
private $width; //自動增長部分的個數
public function __construct($width, $filename, $separate)
{
$this->width = $width;
$this->filename = $filename;
$this->separate = $separate;
}
public function getOrUpdateNumber($current, $start)
{
$record = IOUtil::read_content($this->filename);
$arr = explode($this->separate, $record);
if($current == $arr[0])
{
//如果是同一天,則繼續增長
$arr[1]++;
IOUtil::write_content("$arr[0],$arr[1]", $this->filename); //將新值存入文件中
return "$arr[0]".str_pad($arr[1],$this->width,0,STR_PAD_LEFT);
}
else
{
//如果兩個日期不一樣則重新從起始值開始
$arr[0] = $current;
$arr[1] = $start;
IOUtil::write_content("$arr[0],$arr[1]", $this->filename); //將新值存入文件中
return "$arr[0]".str_pad($arr[1],$this->width,0,STR_PAD_LEFT);
}
}
}
class IOUtil
{
public static function read_content($filename)
{
$handle = fopen($filename,"r");
$content = fread($handle,filesize($filename));
return $content;
}
public static function write_content($content, $filename)
{
$handle = fopen($filename,"w");
fseek($handle,0);
fwrite($handle, $content);
return $content;
}
}
?>
參數含義分別是日期後自增長數的位數, 存儲的文件名稱, 日期與自增長數的分割數注意:網站目錄下需有EveryDaySerialNumber.dat否則會發生錯誤
使用範例(一)
$obj = new FileEveryDaySerialNumber(4,"EveryDaySerialNumber.dat",",");
$current_date = date("Ymd");
echo $obj->getOrUpdateNumber($current_date,1);
輸出結果:
201207230001
使用範例(二)
$obj = new FileEveryDaySerialNumber(3,"EveryDaySerialNumber.dat",",");
$current_date = date("ym");
echo $obj->getOrUpdateNumber($current_date,1);
輸出結果:
1207001
訂閱:
文章 (Atom)