File I/O in PHP

Introduction: This tutorial is on how to perform file I/O in PHP. I/O? I/O stands for Input/Output, and means writing/reading to/from files on the system. PHP Tags: First we need three things; The file you want to write the contents to - this can be pre-existing, or not. The contenst to write to the file. The default PHP tags, ready to write our PHP code. Here are the PHP tags, in which we will write our code...
  1. <?php
  2.  
  3. ?>
Contents: Next, I am going to declare a new variable named 'content' which will have the value of what text I want to write to the file...
  1. $content = "This is my content I am writing to the file.";
Open Files: Now we are ready to attempt opening a file. To do this, we are going to the use the default PHP function 'fopen' to open our file. This function takes two parameters; the file path, an opening mode. There are a few different opening modes; w - Write a - Append r - Read If you would like to overwrite the pre-existing contents of the file, choose the 'w' write mode. If you want to append text to the end of the file, use the 'a' append mode. Finally, if you only want to read the file, use the 'r' read mode. There are also 'c' and 'x' modes, as well as '+' modes of each for additional permissions - these can be found on the official PHP website. I only want to write to the file, and therefore overwrite pre-existing contents and so I am going to use the 'w' write mode...
  1. $handle = fopen('writingfile.txt', 'w');
Please note, both parameters should be given as strings. The above code would attempt to open 'writingfile.txt' with the 'w' write mode permissions. Because there are no forward slashes in the file path, it will look for the file in the current path directory. Attempted? You may of noticed that I keep using the word 'attempt' when it comes to opening the file, that is because if we make a script to open a file that doesn't exist, and then write to it - thinking that it does exist - PHP will throw an error. So, next we want to ensure that the handle variable is set correctly. If the file doesn't exist, the 'fopen' function would return a false value...
  1. if ($handle) {
  2.  
  3. }
Writing: Finally, we are going to use the 'fwrite' function to write to our file. The first parameter is the 'fopen'ed file, in our case it is the '$handle' variable. The second parameter is the contents to write, in this case it is the '$content' variable...
  1. fwrite($handle, $content);
Streams: Finally, we want to close the opened file stream to our 'writingfile.txt' file. This is because if another script attempts to also open the file for writing/reading/appending, it may throw an error...
  1. fclose($handle);
Finished! Here is the full source code...
  1. $content = 'This is my content I am writing to the file.';
  2. $handle = fopen('writingfile.txt', 'w');
  3. if ($handle) {
  4. fwrite($handle, $content);
  5. fclose($handle);
  6. }else
  7. echo 'The file could not be opened.';

Add new comment