PHP 中有各種內建函數,用於對檔案執行各種操作。他們可能會對文件進行建立、開啟、讀取、寫入等操作。
開始您的免費軟體開發課程
網頁開發、程式語言、軟體測試及其他
以下是 PHP 預設可用的主要函數:
首先,為了寫入文件,我們必須知道如何建立該文件。這是在 open() 函數的幫助下完成的。這個名稱可能會誤導您開啟文件,但在 PHP 中,相同的函數用於建立和開啟文件,就像 Linux 中的 vi 函數一樣。該函數一旦執行,就會檢查檔案是否存在,然後僅建立它。下面的範例示範了相同的內容:
代碼:
<?php // Creating a file for test $myfile = fopen("test.txt", "w") ?>
輸出:
建立檔案後,我們需要將所需的內容寫入其中,因此我們使用此功能。只有當到達文件末尾(EOF)或我們根據先到的順序指定的長度時,此函數才會停止。
文法:
fwrite(file, string, length)
代碼:
<?php // Your code here! $f = fopen("testfile.txt", "w"); $text = "Random data here\n"; fwrite($f, $text); ?>
輸出:
這裡 testfile.txt 是建立的文件,指派給 $text 的字串值會寫入該文件。
這是另一個可用來將內容寫入 PHP 檔案的函數。存取文件時需要遵循一定數量的相同順序的規則:
文法:
file_put_contents(filename, text, mode, context)
傳回值: 如果成功,此函數將傳回寫入檔案的總位元組數;如果失敗,則傳回值 FALSE。
以下是範例:
代碼:
<?php echo file_put_contents("filetest.txt","Testing for file write"); ?>
輸出:
這裡我們建立的檔案作為第一個參數給出,下一個參數是寫入該檔案的文字。
我們可以覆寫上面已經寫入資料的檔案。文件中已存在的任何資料都會被清除,並且它將作為一個全新的空文件啟動。在下面的範例中,我們將開啟一個現有文件並嘗試在其上寫入新資料。
以下是範例:
代碼:
<?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); ?>
輸出:
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.
Below are the examples of PHP Write File:
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:
This creates a file TestFile.txt having 2 lines of data as mentioned.
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:
This example appends the given names to the same file as in the first example.
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.
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-
以上是PHP 寫入文件的詳細內容。更多資訊請關注PHP中文網其他相關文章!