If the file does not exist, you create it by using the function touch().
You can write to or read from a existing text file using php.
The following is simple example that illustrates how to create, write, and append information to a text file.
<html>
<title>Create text file </title>
<body>
<?php
$fileName="textFile.txt";
print "Writing to the file $fileName from blank<br/>";
$ft=fopen($fileName, "w") or die ("Unable to open the file $filename");
fwrite($ft,"This is the first line of my text file\n");
fclose($ft);
print "Adding line of text to the file $fileName<br/>";
$ft=fopen($fileName, "a") or die ("Unable to open the file $filename");
fwrite($ft,"This is my second line of text here to add to the previous text\n");
fclose($ft);
?>
</body>
</html>
touch() creates the text file.
fwrite writes the quoted string to the text file.
You can also use fputs() instead of fwrite(). They both do the same thing.
fopen() prepares the file for writing. If you want destroy previous writing and start as blank file use the
argument $fp=$fopen("fileName.txt", "w") or if you want append to existed content of the file, use the argument $fp=$fopen("fileName.txt", "a")
fclose() closes the file.
This example is creating text file, adds line of text, and appends another line of text.
Reading from a Text File with PHP
Reading from a text file is also very easy and similar to writing to it.
The following example illustrates how to read from a text file.
<html>
<title>Create text file </title>
<body>
<?php
$fileName="textFile.txt";
print "Reading from $fileName<br>";
$ft=fopen($fileName, "r") or die ("Unable to open the file $filename");
while(!feof($ft)){
$lines = fgets($ft);
print "$lines<br>";
}
fclose($ft);
?>
</body>
</html>
This example is usefull only when you have line breaks in your text file and displays all the lines of the text file one by one.
fgets() retreives the date up to the line break.
feof() is short for End Of File, which knows the end of file.
We run trough the lines and write each line of text to the browser.
What about if our file does not have line breaks?
To handle that, we can use the function fread() which gets the data in a defined chunk without looking for line break.
The following example uses fread() to read the file.
<html>
<title>Create text file </title>
<body>
<?php
$fileName="textFile.txt";
print "Reading from $fileName<br>";
$ft=fopen($fileName, "r") or die ("Unable to open the file $filename");
while(!feof($ft)){
$data = fread($ft,300);
print "$data<br>";
}
fclose($ft);
?>
</body>
</html>
The other thing that fread() requires is the number of bytes you want to read.
In the above example, we read the file up to 300 bytes.
There are other functions that you can use to process your text file. The following are few more functions.
fseek() sets mannualy where fread() start readinf from in the file.
fgetc() gets only single character from the file.
fileatime() returns the date that the file was last accessed.
file_exist() returns true file if the file exists and false if not.
is_file() tests if the entity is file, returns true if so.
is_dir() tests if the entity is directory, returns true if so.
is_readable(), is_writable(), is_executable() each tests it's function, if the file readable/writable/executable and return true if so.