PHP檔案操作

2021-07-04 13:37:24 字數 989 閱讀 9804

users.txt

ale [email protected]

nicole [email protected]

laura [email protected]

1.識別檔案末尾字元

程式需要一種標準的方式來識別何時到達檔案的末尾。。這個標準通常稱為檔案末尾(或eof)字元。

在php中,此函式是feof()。feof()函式用來確定是否到達資源末尾,它在檔案i/o操作中經常使用。

<?php

$fh = fopen("data/users.txt","r");

//當未達到eof時,檢索下一行

while(!feof($fh))

//關閉檔案

fclose($fh);

?>

ale [email protected] nicole [email protected] laura [email protected]
2.讀取指定數目的字元

fgets()函式返回通過開啟的資源控制代碼讀入的若干個字元,或者返回遇到換行或eof之前讀取的所有內容

string fgets(resource handle [,int length])
如果忽略可選的length引數,則假設為1024個字元。在大多數情況下,這意味著fgets()將在讀取到1024個字元前遇到換行符,因而每次成功呼叫都會返回下一行

<?php

$fh = fopen("data/users.txt","r");

echo fgets($fh);

//關閉檔案

fclose($fh);

?>

ale [email protected]
此例子比上例缺少while迴圈,fget()遇到換行符就停止讀取了。

php操作php檔案

聽起來有些暈吧?使用php來操作php頁面。有乙個小的用途,在乙個系統當中可能會有個別的小項不便存入資料庫,但是我們又要經常在其他頁面當中呼叫,而且還需要更新,那麼我們就可以用這種方式來解決。其中遇到幾個小問題,搞了俺半天時間才搞定 比如說 使用者需要更改某乙個標題,但是單獨為這個標題在建立乙個表,...

php檔案操作

1 開啟檔案 resource fopen string filename,string mode bool use include path resource zcontext handle fopen filename,mode 開啟檔案,返回代表此檔案的資源的控制代碼 檔名稱可以使用相對路徑或...

PHP 檔案操作

建立檔案 方法一 file fopen test.txt w 方法二 touch file 建立資料夾 mkdir web www testing test dir 0700 建立多層資料夾 原生 mkdir web www testing test dir 0700,true 遞迴 functio...