首頁  >  文章  >  後端開發  >  PHP 寫入文件

PHP 寫入文件

WBOY
WBOY原創
2024-08-29 13:02:40304瀏覽

PHP 中有各種內建函數,用於對檔案執行各種操作。他們可能會對文件進行建立、開啟、讀取、寫入等操作。

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

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

PHP 寫入檔案的函數

以下是 PHP 預設可用的主要函數:

1. fopen()

首先,為了寫入文件,我們必須知道如何建立該文件。這是在 open() 函數的幫助下完成的。這個名稱可能會誤導您開啟文件,但在 PHP 中,相同的函數用於建立和開啟文件,就像 Linux 中的 vi 函數一樣。該函數一旦執行,就會檢查檔案是否存在,然後僅建立它。下面的範例示範了相同的內容:

代碼:

<?php
// Creating a file for test
$myfile = fopen("test.txt", "w")
?>

輸出:

PHP 寫入文件

2. fwrite()

建立檔案後,我們需要將所需的內容寫入其中,因此我們使用此功能。只有當到達文件末尾(EOF)或我們根據先到的順序指定的長度時,此函數才會停止。

文法:

fwrite(file, string, length)
  • 其中文件是必填字段,描述我們應該寫入的文件
  • 字串是另一個必要參數,它告訴字串寫入開啟的檔案
  • length 是一個可選參數,它為我們提供了要寫入的最大位元組數

代碼:

<?php
// Your code here!
$f = fopen("testfile.txt", "w");
$text = "Random data here\n";
fwrite($f, $text);
?>

輸出:

PHP 寫入文件

這裡 testfile.txt 是建立的文件,指派給 $text 的字串值會寫入該文件。

3. file_put_contents()

這是另一個可用來將內容寫入 PHP 檔案的函數。存取文件時需要遵循一定數量的相同順序的規則:

  • 有一個名為 FILE_USE_INCLUDE_PATH 的屬性,它在設定時檢查路徑是否包含檔案名稱的副本。
  • 檢查檔案是否存在後建立檔案
  • 接下來,它開啟檔案
  • 如果設定了屬性 LOCK_EX,則會鎖定檔案
  • 當設定屬性 FILE_APPEND 時,它會移到檔案結尾,否則會清除檔案的內容。
  • 現在它將所需的資料寫入檔案。
  • 關閉檔案並釋放(如果有任何鎖定)
注意: 應使用 FILE_APPEND 屬性,以防止資料在將資料附加到文件末尾時被完全擦除。

文法:

file_put_contents(filename, text, mode, context)
  • 其中 filename 是強制參數,告訴我們必須寫入的檔案的完整路徑,因此此函數檢查並建立一個檔案。
  • text 是另一個必填字段,它是我們必須寫入文件的資料。它可以是簡單字串、字串陣列或資料流的形式。
  • mode 是可選字段,它為我們提供了對文件進行操作的多種方式。其可能的值為:
    • FILE_USE_INCLUDE_PATH: 這會在包含目錄路徑中搜尋指定的檔案名稱。
    • FILE_APPEND:它將資料附加到檔案而不是覆蓋相同的資料。
    • LOCK_EX:這會在寫入時對檔案施加明確鎖定。
  • context 是可選參數,它給出檔案的上下文。它基本上是一堆可以改變流行為的選項。

傳回值: 如果成功,此函數將傳回寫入檔案的總位元組數;如果失敗,則傳回值 FALSE。

以下是範例:

代碼:

<?php
echo file_put_contents("filetest.txt","Testing for file write");
?>

輸出:

PHP 寫入文件

這裡我們建立的檔案作為第一個參數給出,下一個參數是寫入該檔案的文字。

4.覆蓋

我們可以覆寫上面已經寫入資料的檔案。文件中已存在的任何資料都會被清除,並且它將作為一個全新的空文件啟動。在下面的範例中,我們將開啟一個現有文件並嘗試在其上寫入新資料。

以下是範例:

代碼:

<?php
$f = fopen("testfile.txt", "w");
$text = "Random data here\n";
$filetext = "Over writing the data\n";
fwrite($f, $filetext);
$filetext = "File Dummy Data\n";
fwrite($f, $filetext);
fclose($f);
?>

輸出:

PHP 寫入文件

Here we are overwriting data in testfile.txt, so whatever is mentioned in the $filetext string value, the same will be written to the file. And when we use the same write command again by changing data given to the $filetext, then the old data is cleaned up and occupied by the latest text value that is provided.

At the end of the file, we always should close it by using the close() function which we have opened using the fwrite() function. As shown in the above example, we also use the \n, which represents the newline equivalent to pressing the enter button on our keyboard.

Now let us take an example and see how to include more data in our file.

Examples to Implement of PHP Write File

Below are the examples of PHP Write File:

Example #1

First, we add 2 lines of data using the below code:

Code:

<?php
$testf = "TestFile.txt";
$filehandle = fopen($testf, 'w');
$fdata = "Julian Caesar\n";
fwrite($filehandle, $fdata);
$fdata = "Harry James\n";
fwrite($filehandle, $fdata);
print "Data writing completed";
fclose($filehandle);
?>

Output:

PHP 寫入文件

This creates a file TestFile.txt having 2 lines of data as mentioned.

Example #2

We will append another 2 names in the same file as shown below:

Code:

<?php
$testf = "TestFile.txt";
$filehandle = fopen($testf, 'a');
$fdata = "Lilly Potter\n";
fwrite($filehandle, $fdata);
$fdata = "Edward Cullen\n";
fwrite($filehandle, $fdata);
print "Data has been appended";
fclose($filehandle);
?>

Output:

PHP 寫入文件

This example appends the given names to the same file as in the first example.

Conclusion

As shown above, there are various methods and steps that need to be followed when we want to write to a file in PHP. fwrite() is one of the main functions to do this and is used majorly for writing data to a file. They can do basic writing of data to our file but should be used in combination with other mandatory functions like open() and close(), without which it is not possible to perform operations on the file if it does not exist.

Recommended Article

This is a guide to the PHP Write File. Here we discuss the Introduction and its Functions of PHP Write File along with examples and Code Implementation. You can also go through our other suggested articles to learn more-

  1. Overview of Abstract Class in Python
  2. What is Abstract Class in PHP?
  3. Socket Programming in PHP with Methods
  4. PHP chop() | How to Work?

以上是PHP 寫入文件的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
上一篇:PHP讀取文件下一篇:PHP讀取文件