In this chapter, we\’ll show you how to make a file on the server and write to it.
PHP Create File – fopen ()
A file can also be made with the fopen() function. In PHP, the same function that is used to open files is also used to create new files.
If you use fopen() on a file that doesn\’t exist, it will create it if the file is opened for writing (w) or appending (a) (a).
In the next example, a new file called \”testfile.txt\” is made. The file will be made in the same folder that contains the PHP code:
Example
$newfile = fopen (\”testfilesystem.txt\”, \”w\”)
PHP File Permissions
If you\’re getting errors when you try to run this code, make sure you\’ve given your PHP file permission to write to the hard drive.
PHP File Write – fwrite ()
You can write to a file with the fwrite() function.
The name of the file to write to is the first parameter of fwrite(). The string to be written is the second parameter.
In the following example, two names are written to a new file called \”testfilesystem.txt\”:
Example
<?php
$newfile = fopen(\”testfilesystem.txt\”, \”w\”) or die(\”Unable to open file! \”);$txt = \”coderazaa.com\”;
fwrite($newfile , $txt);
$txt = \”seotoolking.com\”;
fwrite($newfile , $txt);
fclose($newfile ); ?>
Take note that we wrote twice to the file \”testfilesystem.txt.\” Every time we wrote to the file, we sent the string $txt that had \”coderazaa.com\” and \”seotoolking.com\” in it. After we were done writing, we used the fclose() function to close the file.
The \”testfilesystem.txt\” file would look like this if we opened it:
Output
coderazaa.com
seotoolking.com
PHP Overwriting
Now that \”testfilesystem.txt\” has some information in it, we can show what happens when we open a file that already exists to write to it. All of the current information will be ERASED, and we\’ll start with a blank file.
In the example below, we open a file called \”testfilesystem.txt\” that already exists and add some new information to it:
Example
<?php
$newfile = fopen(\”testfilesystem.txt\”, \”w\”) or die(\”Unable to open file! \”);$txt = \”freevideoconvertor.com\”;
fwrite($newfile , $txt);
$txt = \”moralstory.in\”;
fwrite($newfile , $txt);
fclose($newfile );?>
When we open the \”testfilesystem.txt\” file now, neither John nor Jane are there. Only the data we just wrote is there:
Output
freevideoconvertor.com
moralstory.in
PHP Append Text
With the \”a\” mode, you can add data to a file. The \”a\” mode adds text to the end of the file, while the \”w\” mode replaces the file\’s old content and deletes it.
In the example below, we open a file called \”testfilesystem.txt\” that already exists and add some text to it:
Example
<?php
$myfile = fopen(\”testfilesystem.txt\”, \”a\”) or die(\”Unable to open file! \”);$var= \”Coderazaa.com\”;
fwrite($myfile, $var);
$var= \”seotoolking.com\”;
fwrite($myfile, $var);
fclose($myfile);?>
If we now open the \”testfilesystem.txt\” file, we will see that Coderazaa.com and seotoolking.com have been added to the end of the file:
Output
freevideoconvertor.com
smoralstory.in
Coderazaa.com
seotoolking.com