Opening, Writing, Appending, Closing, and Reading Files in PHP
There may be times when you need to open a file for reading, writing, or appending it. Using the PHP Filesystem function of fopen() gives us options when opening a file. You simply choose the mode that you want when you execute the fopen() function. The three most widely used modes are "w", "a", and "r". After the file is opened we use fwrite() to place the data we desire into it. We also utilize the fclose()function to close the file handler.
It is important to note that any time you use several of the modes above, the target file will be created on server if it does not yet exist. Which makes it operate as a file creation method like the touch() function.
Here is a code example showing how to use a couple of the modes above for reading and writing files:
In the code example above we created a file, wrote to it, closed it, appended data to it, then displayed its contents.
Mode | Mode Functionality |
w | Write to a file. If the file does not exist, attempt to create it. |
w+ | Reading and writing to a file. If the file does not exist, attempt to create it. |
a | Write to a file. If the file does not exist, attempt to create it. |
a+ | Reading and writing. If the file does not exist, attempt to create it. |
r | Reading file only. |
r+ | Reading and writing to a file. |
x | Create and open for writing only. If the file already exists, fopen() will fail by returning FALSE and generating an error. If the file does not exist, attempt to create it. |
x+ | Create and open for reading and writing. If the file already exists, fopen() will fail by returning FALSE and generating an error. If the file does not exist, attempt to create it. |
It is important to note that any time you use several of the modes above, the target file will be created on server if it does not yet exist. Which makes it operate as a file creation method like the touch() function.
Here is a code example showing how to use a couple of the modes above for reading and writing files:
<?php // Here we define the file path and name $target_file = "my_file.txt"; // Here we define the string data that is to be placed into the file $target_file_data = "This is the string data or code I want to place in the newly created file.";// Here we are creating a file(since it does not yet exist) and adding data to it$handle = fopen($target_file, "w"); fwrite($handle, $target_file_data); // write it fclose($handle); // Here we are opening and appending to the file $handle = fopen($target_file, "a"); // Here we define the string data that is to be appended to the data already in file$target_file_data = "Here is more data I want to append to the file.";fwrite($handle, $target_file_data); // write it fclose($handle);
// Here we display the file contents by including it include($target_file); ?>
This is the string data or code I want to place in the newly created file. Here is more data I want to append to the file.
In the code example above we created a file, wrote to it, closed it, appended data to it, then displayed its contents.
https://www.developphp.com/page.php?id=237