網頁

2011年7月15日 星期五

【教學】php 檔案存取

檔案、存取

名稱用途
basename(完整路徑的檔案)傳回該含路徑檔案的檔名basename("/home/httpd/index.php");
//index.php
dirname(完整路徑的檔案)傳回該含路徑檔案的路徑basename("/home/httpd/index.php");
//home/httpd
檔案存取
file(檔案)讀檔案內容至陣列中$lines = file("data.txt");
//$lines為一陣列
readfile(檔案)讀入一個檔案,然後從stdout輸出
單純做dump用
readfile("/etc/passwd");
fpassthru(檔案指向)從檔案指向所在位置,讀到檔尾
然後從stdout輸出
$fp = fopen("/etc/passwd", "r");
fpassthru($fp);
fopen(檔案, 模式)開啟檔案, 模式:
r - 唯讀
r+ -讀寫
w -寫,覆蓋,檔案不存在自動建立
w+ - 讀寫,覆蓋,檔案不存在自動建立
a - 寫,加在最後
,檔案不存在自動建立
a+ - 讀寫,加在最後,檔案不存在自動建立
$fp = fopen("test", "a");
flock(檔案指向, 模式)鎖定檔案,模式:
1 - lock as reader(shared lock)
2 - lock as writer(exclusive lock)
3 - release
flock($fp, 1);
fputs(檔案指向, 字串[, 長度])寫入內容,可指定寫入長度
未指定時表整個字串
fput($fp, "mydata");
fwrite(檔案指向, 字串[, 長度])同fputs
fread(檔案指向長度)Binary-safe file read,遇到EOF或長度時停止$content = fread($fp, filesize($filename));
fclose(檔案指向)關閉檔案fclose($fp);
feof(檔案指向)指向位置是否為EOFfeof($fp);
fseek(檔案指向位移量)移動,成功傳回0,否則傳回-1fseek($fp, 5);
rewind(檔案指向)移到檔案開頭rewind($fp);
fgetc(檔案指向)取回字元fgetc($fp);
fgets(檔案指向長度)取回(長度-1)個字元,
遇到EOF或new line會中斷
fgets($fp, 10);
fgets(檔案指向長度)取回(長度-1)個字元,
遇到EOF或new line會中斷
※會去除標記及<? ...?>
fgetss($fp, 10);
popen(檔案, 模式)開啟檔案, 模式:
r - 唯讀
w -寫,覆蓋,不會自動建立
$fp = popen("test", "w");
pclose(檔案指向)關閉用popen開啟的檔案pclose($fp);
檔案管理
copy(來源, 目的)複製檔案copy("t1.php", "t2.php");
//未指定路徑,用目前路徑
rename(舊檔名, 新檔名)重新命名rename("t2.php", "t3.php")
//未指定路徑,用目前路徑
unlink(檔案)刪除檔案unlink("t3.php");
//未指定路徑,用目前路徑
file_exists(檔案)檢查檔案是否存在file_exists("t3.php");
//未指定路徑,用目前路徑
mkdir(目錄名稱, 權限)
※權限必須用四位數之8進位
建立目錄mkdir("testdir", "0700");
rmdir(目錄名稱)刪除目錄rmdir("testdir");
檢查
is_dir(目錄)是否為目錄is_dir("testdir")
is_file(檔案)是否為(reqular)檔案is_file("test")
is_readable(檔案)是否可讀取is_readable("test");
is_writeable(檔案)是否可寫入is_writeable("test");
is_executable(檔案)是否可執行is_executable("test");
權限
chgrp(檔案, 群組)改變群組chgrp("/tmp/test", "somegroup");
chmod(檔案, 執行權)改變執行權
※必須用4個八進位數字,
注意不要加雙引號
chgrp("/tmp/test", 0666);
chown(檔案, 使用者)改變使用者chown("/tmp/test", "someuser");
資訊
fileatime(檔案)最後一次存取時間,傳回timestamp 
※開檔未更動也算存取
$t = fileatime("p1.php");
echo date("Y-m-d H:i:s", $t);
filectime(檔案)最後一次更動時間,傳回timestamp$t = filectime("p1.php");
echo date("Y-m-d H:i:s", $t);
filemtime(檔案)最後一次被修改時間,傳回timestamp$t = filemtime("p1.php");
echo date("Y-m-d H:i:s", $t);
filesize(檔案)檔案大小filesize("test");
filetype(檔案)檔案類型, 可能值:
fifo, char, dir, block, link, file, unknown
filetype("test");
fileowner(檔案)檔案擁有者的IDfileowner("test");
filegroup(檔案)檔案群組的IDfilegroup("test");
fileinode(檔案)檔案的inode numberfileinode("test");
fileperms(檔案)檔案的permissionsfileperms("test");
其他
tempname(路徑, 檔名前文字)
※檔名前文字至多5個字元
傳回目錄下的唯一檔名
路徑不存在,則為/tmp

 本文引用自 http://class.ylc.edu.tw/~u09/phpclass/func/file.htm

沒有留言:

張貼留言

歡迎參觀我的部落格!純屬娛樂 如有侵權 請告知我會立即移除sportswingblog@gmail.com