PHP - File Write
Now that you know how to open and close a file, lets get on to the most useful part of file manipulation, writing! There is really only one main function that is used to write and it's logically called fwrite.
Advertise on Tizag.com
PHP - File Open: Write
Before we can write information to our test file we have to use the function fopen to open the file for writing.
PHP Code:$myFile = "testFile.txt"; $fh = fopen($myFile, 'w');
PHP - File Write: fwrite Function
We can use php to write to a text file. The fwrite function allows data to be written to any type of file. Fwrite's first parameter is the file handle and its second parameter is the string of data that is to be written. Just give the function those two bits of information and you're good to go!
Below we are writing a couple of names into our test file testFile.txt and separating them with a carriaged return.
PHP Code:$myFile = "testFile.txt"; $fh = fopen($myFile, 'w') or die("can't open file"); $stringData = "Bobby Bopper\n"; fwrite($fh, $stringData); $stringData = "Tracy Tanner\n"; fwrite($fh, $stringData); fclose($fh);
The $fh variable contains the file handle for testFile.txt. The file handle knows the current file pointer, which for writing, starts out at the beginning of the file.
We wrote to the file testFile.txt twice. Each time we wrote to the file we sent the string $stringData that first contained Bobby Bopper and second contained Tracy Tanner. After we finished writing we closed the file using the fclose function.
If you were to open the testFile.txt file in NOTEPAD it would look like this:
Contents of the testFile.txt File:
PHP - File Write: Overwriting
Now that testFile.txt contains some data we can demonstrate what happens when you open an existing file for writing. All the data contained in the file is wiped clean and you start with an empty file. In this example we open our existing file testFile.txt and write some new data into it.
PHP Code:$myFile = "testFile.txt"; $fh = fopen($myFile, 'w') or die("can't open file"); $stringData = "Floppy Jalopy\n"; fwrite($fh, $stringData); $stringData = "Pointy Pinto\n"; fwrite($fh, $stringData); fclose($fh);
If you now open the testFile.txt file you will see that Bobby and Tracy have both vanished, as we expected, and only the data we just wrote is present.
Contents of the testFile.txt File:
In the next lesson we will show you how to get information out of a file by using PHP's read data functions!
Download Tizag.com's PHP Book
If you would rather download the PDF of this tutorial, check out our PHP eBook from the Tizag.com store. Print it out, write all over it, post your favorite lessons all over your wall!
Found Something Wrong in this Lesson?
Report a Bug or Comment on This Lesson - Your input is what keeps Tizag improving with time!
Time to try our luck with reading and writing to files in PHP. PHP actually makes it really easy for us to do whatever we want with files; reading, writing, and deleting. We will first start off with reading a file.
Reading a file
We start off by setting our file name with the $myFile set to “sampleFile.txt”. Then we open our established link with the $myFileLink variable by setting our file name and what we what to do with the file (r for read). We then move onto the most important line where we get the contents of the file with our variable $myFileContents. We use the fread function, and pass in our variable link as the first parameter followed by the number of characters we want to read (in this case 21 characters). PHP reads files similar to how you would. It starts with the beginning and proceeds through the file, so always remember you are starting from the beginning unless you tell it not to.
Now that is great and all to simply read the first 21 characters of a file, but what if we don’t know how many characters we want to read? We need to figure out a way to tell PHP the number of characters that are in the entire file. Good thing PHP already has a function that can help us.
The main difference between this example and the previous one is the filesize function. The filesize function simply gets the size of the file, which means we can use it to tell us the number of characters that are in the file. By inputting that function into the fread function we can tell PHP to read to the end of the file. You could also subtract from the filesize function if you wanted to leave off so many characters.
Writing to a file
No worries, writing to a file isn’t that much harder than reading from one. It is just a lot more dangerous, which is perfectly fine as long as you know what you are doing. So, let’s start writing to a file.
Remember that writing to a file will erase it immediately. So, if you are going to use the write function make sure you run it on some test content before you wipe out all of your important files. QUICK NOTE: make sure you have write permissions set on the folder. If the write permissions are not enable, the server will tell PHP that it cannot write to that file. We open the file link just like normal. Then, we set the variable $newContents to hold the content we want to write to the file. Finally, we use the fwrite function to write to our $newContents to our $myFileLink2. And of course, we close of file link like a good web developer that cares about his server.
Writing to the end of a file
We can write to the end of the file, known as appending. Using the previous example, you can simply change the w+ to an a. Append is exactly like write, but except it keeps the current contents and adds new contents to the end. Appending is much safer than using write, but sometimes you must write over a file. Please be careful.