Time to try our luck with PHP Read and Write to Files. 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.
PHP Read and Write to File
Reading a file
$myFile = "sampleFile.txt"; $fh = fopen($myFile, 'r'); $myFileContents = fread($fh, 21); fclose($fh); echo $myFileContents;
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.
$myFile = "sampleFile.txt"; $myFileLink = fopen($myFile, 'r'); $myFileContents = fread($myFileLink, filesize($myFile)); fclose($myFileLink); echo $myFileContents;
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.
$myFile2 = "testFolder/sampleFile2.txt"; $myFileLink2 = fopen($myFile2, 'w+') or die("Can't open file."); $newContents = "You wrote on me..."; fwrite($myFileLink2, $newContents); fclose($myFileLink2);
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 Read and Write to Files 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 learn PHP Read and Write to Files, 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.