在 PHP 中,可以使用 fopen()
函數(shù)來打開文件,使用 fclose()
函數(shù)來關(guān)閉文件。
以下是打開和關(guān)閉文件的示例代碼:
// 打開文件
$file = fopen("example.txt", "r") or die("Unable to open file!");
// 讀取文件內(nèi)容
$content = fread($file, filesize("example.txt"));
echo $content;
// 關(guān)閉文件
fclose($file);
在上面的示例中,fopen()
函數(shù)用于打開名為 example.txt
的文件,并使用 "r"
模式讀取文件內(nèi)容。如果無法打開文件,則使用 die()
函數(shù)顯示錯誤消息。
fread()
函數(shù)用于讀取文件內(nèi)容,filesize()
函數(shù)用于獲取文件大小。
最后,使用 fclose()
函數(shù)關(guān)閉文件。