PHP 檔案取得內容

PHPz
發布: 2024-08-29 12:35:16
原創
428 人瀏覽過

每當我們想要將檔案的內容讀入 PHP 中的字串時,我們都會使用一個名為 file_get_contents() 的函數。如果作業系統支持,該功能可以使用記憶體映射技術來提高效能。成功存取和讀取指定檔案的內容後,函數將檔案的內容傳回字串中。如果函數無法存取指定檔案或讀取指定檔案的內容,則該函數傳回 false,並將要讀取的檔案所在位置的路徑作為參數傳遞給該函數.

廣告 該類別中的熱門課程 精通內容行銷:成功策略 - 專業化 | 4門課程系列

開始您的免費軟體開發課程

網頁開發、程式語言、軟體測試及其他

PHP 中宣告 file_get_contents() 函數的語法如下:

file_get_contents(path_to_the_file);
登入後複製

其中 path_to_the_file 是檔案所在的檔案的路徑,其內容將由 file_get_contents() 函數讀入字串。

PHP 中 file_get_contents() 函數的工作原理

  • 每當我們想要在 PHP 中將檔案的內容讀入字串時,我們都會使用一個名為 file_get_contents() 的函數。
  • 要讀取的檔案所在位置的路徑會作為參數傳遞給 file_get_contents() 函數。
  • 成功存取並讀取指定檔案的內容後,file_get_contents() 函數會將檔案的內容傳回字串中。
  • 如果 file_get_contents() 函數無法存取指定檔案或讀取指定檔案的內容,則函數傳回 false。
  • 如果作業系統支持,file_get_contents() 函數可以使用記憶體映射技術來提高效能。

PHP file_get_contents 範例

以下是下面提到的範例

範例#1

PHP 程式讀取所在位置的檔案內容,其路徑會作為參數傳遞給 file_get_contents() 函數:

代碼:

#making use of file_get_contents() function by passing the path to the file where the file is present to read the contents of the file into a string called filecontents
@filecontents = file_get_contents('C:/Users/admin/Desktop/check.txt');
print "The contents of the specified file is as follows:\n";
#displaying the contents of the file
echo $filecontents;
登入後複製

上述程式的輸出如下圖:

PHP 檔案取得內容

在上面的程式中,我們透過傳遞檔案所在的檔案路徑來使用 file_get_contents() 函數,將檔案的內容讀取到名為 filecontents 的字串中。然後文件的內容將作為輸出顯示在螢幕上。

範例#2

PHP 程式讀取所在位置的檔案內容,其路徑會作為參數傳遞給 file_get_contents() 函數:

代碼:

#making use of file_get_contents() function by passing the path to the file where the file is present to read the contents of the file into a string called filecontents
@filecontents = file_get_contents('C:/Users/admin/Desktop/EDUCBA/check1.txt’);
print "The contents of the specified file is as follows:\n";
#displaying the contents of the file
echo $filecontents;
登入後複製

上述程式的輸出如下圖:

PHP 檔案取得內容

在上面的程式中,我們透過傳遞檔案所在的檔案路徑來使用 file_get_contents() 函數,將檔案的內容讀取到名為 filecontents 的字串中。然後文件的內容將作為輸出顯示在螢幕上。

範例 #3

PHP 程式讀取所在位置的檔案內容,其路徑會作為參數傳遞給 file_get_contents() 函數:

代碼:

#making use of file_get_contents() function by passing the path to the file where the file is present to read the contents of the file into a string called filecontents
@filecontents = file_get_contents('C:/Users/admin/Desktop/check2.txt');
print "The contents of the specified file is as follows:\n";
#displaying the contents of the file
echo $filecontents;
登入後複製

上述程式的輸出如下圖:

PHP 檔案取得內容

在上面的程式中,我們透過傳遞檔案所在的檔案路徑來使用 file_get_contents() 函數,將檔案的內容讀取到名為 filecontents 的字串中。然後文件的內容將作為輸出顯示在螢幕上。

範例#4

PHP 程式讀取所在位置的檔案內容,其路徑會作為參數傳遞給 file_get_contents() 函數:

代碼:

#making use of file_get_contents() function by passing the path to the file where the file is present to read the contents of the file into a string called filecontents
@filecontents = file_get_contents('C:/Users/admin/Desktop/check3.txt');
print "The contents of the specified file is as follows:\n";
#displaying the contents of the file
echo $filecontents;
登入後複製

上述程式的輸出如下圖:

PHP 檔案取得內容

在上面的程式中,我們透過傳遞檔案所在的檔案路徑來使用 file_get_contents() 函數,將檔案的內容讀取到名為 filecontents 的字串中。然後文件的內容將作為輸出顯示在螢幕上。

Example #5

PHP program to read the contents of the file present in the location whose path is passed as a parameter to the file_get_contents() function:

Code:

#making use of file_get_contents() function by passing the path to the file where the file is present to read the contents of the file into a string called filecontents
@filecontents = file_get_contents('C:/Users/admin/Desktop/check4.txt');
print "The contents of the specified file is as follows:\n";
#displaying the contents of the file
echo $filecontents;
登入後複製

The output of the above program is as shown in the snapshot below:

PHP 檔案取得內容

In the above program, we are making use of the file_get_contents() function by passing the path of the file where the files are present to read the contents of the file into a string called filecontents. Then the contents of the file are displayed as the output on the screen.

以上是PHP 檔案取得內容的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
php
來源:php
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!