Read and Write to File

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

Example

Result

I am the sample 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.

Example

Result

I am the sample file…Back off me.

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.

Example

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.

If you enjoyed this resource, support me by sharing this page with others.
Share on Facebook
Facebook
Share on Google+
Google+
Share on StumbleUpon
StumbleUpon
Digg this
Digg
Tweet about this on Twitter
Twitter
Share on Reddit
Reddit

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.