To write content to a file we can use Show Write to a File with PHP
Let's take an example, where we will write a couple of movie names to our file movies.txt
In the code above we wrote two movie names in the file movies.txt. f we open the file, it will look like following: The Man from Earth SouthPaw NOTE: When a file is opened in write mode, all the existing data in the file is erased and new data can be written to the file using the If we again open the above file to write more content to it, and we open the file in write mode then all the existing content will be erased. Append data to a File with PHPIf we wish to add more movie names to the file movies.txt then we need to open the file in append mode. Let's take an example and see. So far we have learned how to open, close, read, and write to a file. However, the ways in which we have written to a file so far have caused the data that was stored in the file to be deleted. If you want to append to a file, that is, add on to the existing data, then you need to open the file in append mode. If we want to add on to a file we need to open it up in append mode. The code below does just that. PHP Code:$myFile = "testFile.txt"; $fh = fopen($myFile, 'a'); If we were to write to the file it would begin writing data at the end of the file. Using the testFile.txt file we created in the File Write lesson , we are going to append on some more data. PHP Code:$myFile = "testFile.txt"; $fh = fopen($myFile, 'a') or die("can't open file"); $stringData = "New Stuff 1\n"; fwrite($fh, $stringData); $stringData = "New Stuff 2\n"; fwrite($fh, $stringData); fclose($fh); You should noticed that the way we write data to the file is exactly the same as in the Write lesson. The only thing that is different is that the file pointer is placed at the end of the file in append mode, so all data is added to the end of the file. The contents of the file testFile.txt would now look like this: Contents of the testFile.txt File:Floppy Jalopy The above example may not seem very useful, but appending data onto a file is actually used everyday. Almost all web servers have a log of some sort. These various logs keep track of all kinds of information, such as: errors, visitors, and even files that are installed on the machine. A log is basically used to document events that occur over a period of time, rather than all at once. Logs: a perfect use for append! If you would rather download the PDF of this tutorial, check out our PHP eBook from the Tizag.com store. Print it out, write all over it, post your favorite lessons all over your wall!
If Parametersfilename Path to the file where to write the data. data The data to write. Can be either a string, an array or a stream resource. If You can also specify the $data 2The value of $data 5Search for filename in the include directory. See for more information.FILE_APPEND If file filename already exists, append the data to the file instead of overwriting it.$data 9Acquire an exclusive lock on the file while proceeding to the writing. In other words, a flock() call happens between the fopen() call and the fwrite() call. This is not identical to an fopen() call with mode "x".$flags 0A valid context resource created with stream_context_create(). Return ValuesThis function returns the number of bytes that were written to the file, or Warning This function may return Boolean ExamplesExample #1 Simple usage example
Example #2 Using flags
Notes
Tip A URL can be used as a filename with this function if the have been enabled. See fopen() for more details on how to specify the filename. See the Supported Protocols and Wrappers for links to information about what abilities the various wrappers have, notes on their usage, and information on any predefined variables they may provide. How do I open file in append mode?In order to append a new line your existing file, you need to open the file in append mode , by setting "a" or "ab" as the mode. When you open with "a" mode , the write position will always be at the end of the file (an append).
How to append a file in PHP?PHP Append Text
You can append data to a file by using the "a" mode. The "a" mode appends text to the end of the file, while the "w" mode overrides (and erases) the old content of the file.
How do I open a file in PHP?PHP fopen() function is used to open file or URL and returns resource. The fopen() function accepts two arguments: $filename and $mode. The $filename represents the file to be opended and $mode represents the file mode for example read-only, read-write, write-only etc.
How to read file into string in PHP?The file_get_contents() reads a file into a string. This function is the preferred way to read the contents of a file into a string. It will use memory mapping techniques, if this is supported by the server, to enhance performance.
|