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中文网其他相关文章!