PHP 寫入文件 file_put_contents

2009年11月17日 junk 尚無評論

在 PHP 版本 5.0.0 之前 file_put_contents 這個函數,必須使用fwrite寫法。

1
2
3
4
5
6
<?php
$fp = fopen("data.txt","w");  //必須要先用fopen開啟文件
$string = "Hello World";  //字串
fwrite($fp, $string);  //用fwrite寫入
fclose($fp);  //關閉檔案
?>


使用 file_put_contents

1
2
3
4
5
<?php
$fp="data.txt";
$string = "Hello World";  //字串
file_put_contents($fp, $string); 
?>


fwrite 寫入文件未端,只要把 $mode 參數 w 改成 a 就好。

1
2
3
4
5
6
<?php
$fp = fopen("data.txt","a");  //必須要先用fopen開啟文件
$string = "Hello World";  //字串
fwrite($fp, $string);  //用fwrite寫入
fclose($fp);  //關閉檔案
?>

由於 file_put_contents 沒有 $mode 參數可以設定,所以需要使用 file_get_contents 取得文件內容。

1
2
3
4
5
6
<?php
$fp="data.txt";
$string=file_get_contents($fp);   //先取出文件內容,轉成字串
$string.="Hello World";            //在字串最後加入新字串
file_put_contents($fp, $string);    //寫入文件
?>
Categories: PHP Tags: , ,

PHP 控制流程 goto

2009年11月8日 junk 尚無評論

PHP 5.3 新增的一個 goto 程式碼。
GOTO 也不是完全好用,詳見 wili 對於goto使用的批評
在 PHP 中 GOTO 限制不能跳進迴圈或switch 中。

1
2
3
4
5
6
7
8
9
&lt;?php
goto loop;
for($i=0,$j=50; $i<100; $i++) {
  while($j--) {
    loop:
  }
}
echo "$i = $i";
?>

PHP官方附GOto
php goto

Categories: Uncategorized Tags:

PHP TmpPHP

2009年10月25日 junk 尚無評論

下載 TmpPHP
這是一時興起所寫的程式,主要是我有時候會想寫一段小程式來try一些函數執行結果,要開啟編輯器然後又亂命名,還要經過上傳測試,所以就會有本垃圾的程式誕生…..用垃圾寫垃圾xd..

或許以後可以拿來寫一段範例執行結果。
本程式無demo,也無登入管理,所以建議使用可以用.htpasswd 加入密碼存取

Categories: PHP Tags: ,

PHP 亂數密碼產生器

2009年10月22日 junk 尚無評論

每次想密碼很麻煩,後來就乾脆自己寫一段,實際上強式密碼我也不常用所以沒加入產生。

<?php
//亂數密碼產生器
function generatorPassword($pt=8,$myWord=""){
	$password="";
	$str="0123456789abcdefghijklmnopqrstuvwxyz";
	$str.=$myWord;
	$str_len=strlen($str);
for ($i=1;$i<=$pt;$i++){
		$rg=rand()%$str_len;
		$password.=$str{$rg};
	}
return $password;
}
//避開強式密碼 < 在html消失
//header("Content-Type: text;"); 

echo generatorPassword();

/*
預設八個字元,設定10個字元
generatorPassword(10,<);
*/
?>
Categories: PHP Tags: , , ,

Adobe Dreamweaver 線上手冊

2009年10月20日 junk 尚無評論

Adobe 官方有提供線上說明文件內容相當豐富
Adobe Dreamweaver CS3
Adobe Dreamweaver CS4
官方 Adobe Dreamweaver CS4 還有 PDF 檔案下載
下載 Adobe Dreamweaver CS4 PDF 文件

Categories: 網頁設計纇 Tags: , , ,

Hello world!

2009年10月19日 admin 尚無評論

Welcome to WordPress. This is your first post. Edit or delete it, then start blogging!

Categories: Uncategorized Tags: