PHP readfile 和 file_get_contents 函式


'readfile' 函式是 PHP 中的一個內建函式,該函式直接將檔案讀取到輸出緩衝區中。函式的引數是檔名。在成功讀取資料的情況下,該函式返回讀取位元組數。否則,返回 FALSE −

示例

 線上演示

<?php
// writing file contents on the output
// buffer using readfile() function
$myfile = @readfile("gfg.txt");
if (!$myfile) {
   print "Sorry, the file could not be opened";
}
?>

輸出

這會輸出如下內容 −

Sorry, the file could not be opened

'file_get_contents' 函式是 PHP 中的一個內建函式,該函式將檔案載入到記憶體中,且只有在呼叫 echo 函式時才會顯示內容。在這個階段,資料會從記憶體複製到輸出緩衝區,然後顯示。該函式利用記憶體對映技術,使其成為讀取檔案內容的有效方式。

需要讀取的檔案路徑作為引數傳遞。如果成功,該函式會返回從檔案中讀取的資料作為輸出;否則,返回 FALSE −

<?php
// reading 36 bytes startig from
// the 0th character from gfg.txt
$text = file_get_contents(‘text_file_name.txt', FALSE, NULL, 0, 36);
echo $text;
?>

更新於:2020 年 4 月 9 日

858 次瀏覽

職業生涯起航

完成課程即可獲取認證

開始
廣告
© . All rights reserved.