File operations in PHP
In PHP, like Python, there is no need to import a library to read and write files. It is handled natively in the language. In some languages such as Java, we must import classes in order to write to a file, such as FileWriter. The first thing we need to do is use PHP's built-in fopen() function to get a file handler. When you use the fopen() function, it returns something called a file handler or file object. Unlike Python, PHP's file handler does not have any built in methods but PHP does have file system methods that we can use such as fread() and fwrite(). Similar to Python, we use to fopen() method to create as well as access a file depending on the mode. PHP builds upon the 4 main modes that Python has: read, write, append and read/write. In Python, you can't define where we want the file pointer to be, but with PHP we can have the file pointer at the beginning of the file or ant the end of a file Below are the file object modes :
"r" Read only. Starts at the beginning of the file "r+" Read/Write. Starts at the beginning of the file "w" Write only. Opens and clears the contents of file; or creates a new file if it doesn't exist "w+" Read/Write. Opens and clears the contents of file; or creates a new file if it doesn't exist "a" Write only. Opens and writes to the end of the file or creates a new file if it doesn't exist "a+" Read/Write. Preserves file content by writing to the end of the file "x" Write only. Creates a new file. Returns FALSE and an error if file already exists "x+" Read/Write. Creates a new file. Returns FALSE and an error if file already exists
Read
To open a file, PHP uses the following syntax :
file_object = fopen( FILENAME,MODE,INCLUDEPATH,CONTEXT )
PHP, as well as other languages such as Java and Python, differentiated between text files and binary files. To open a text file in PHP, we can use the 't' context flag or not include it since it is the default mode. To open a binary file in PHP, we must use the 'b' context flag to tell the file object that this is a binary file and the language won't translate the data in the file. In Java, however, we must import a different set of libraries such as FileInputStream and FileOutputStream.
To read from a text file, we will need to create a file handler using the fopen method. PHP provides four different functions to read from a file: readfile(), fread() and
This is the first line of text. This is the second line of text.
readfile() :
echo readfile("foo.txt");
Will return :
This is the first line of text. This is the second line of text. 63
fread() :
$file = fopen("foo.txt","r"); $text=fread($file); echo $text ; fclose($file);
Will return :
This is the first line of text. This is the second line of text.
$file = fopen("foo.txt","r"); $text=fread($file, "4"); echo $text ; fclose($file);
Will return :
This
fgets() :
$file = fopen("foo.txt","r"); echo fgets($file) ; echo $text ; fclose($file);
Will return :
This is the first line of text.
file()
print_r(file("test.txt"));
Will return :
Array ( [0] => This is the first line of text. [1] => This is the second line of text. )
Write
To write to a file, we will use the PHP fwrite() method. This method will write to the open file and return the number of bytes written. In PHP, fwrite() is binary-safe; meaning that both binary data, like images, and character data can be written unlike in Python.
$file = fopen("foo.txt","w"); echo fwrite($file,"Hello World"); fclose($file);
returns 11
Remember to close the file object
Once we are done with all the file input / output operations that we need to do, we must close the file object. To do so, we would use the fclose() method. Like Java and Python, the file can only have one file object at a time pointing to it. Here is some code to illustrate the need to close the file.
1 $file = fopen( "foo.txt", "w") 2 3 fwrite($file,"Hello World" ) 4 5 $file2 = fopen( "foo.txt", "w" ) 6 fwrite($file2,"Good Bye") 7 fclose($file) 8 fclose($file2)
The code above will produce a file that has "Hello World" not "Good Bye."
1 $file = fopen( "foo.txt", "w") 2 3 fwrite($file,"Hello World" ) 4 5 fclose($file) 5 file_object2 = open( "foo.txt", "w" ) 6 fwrite($file2,"Good Bye") 7 fclose($file2)
The code above will produce a file that has "Good Bye."