Build in-demand skills in Northwestern’s online MS in Information Design
by Sajal Soni21 hours ago Difficulty:BeginnerLength:MediumLanguages: In this tutorial, we are going to learn file handling in PHP. I’ll show you how to create, read, write, and delete files in PHP by using the built-in file handling functions. File handling is something that you will need to do very often as a PHP developer. You could use PHP file handling functions to manipulate files in different ways. These functions could be used to build features in your applications that range from custom error logging to storing cached files. Examples of utility tools that you could build with these functions are: custom logging and debugging tools application configuration storage front-end and application caching localization support and many more PHP provides several file handling functions that allow you to perform various operations, like: create and open a file write into a file read from a file delete a file close a file Today, we’ll go through each and every file operation, along with examples of how to use them. I would encourage you to try out the examples in this tutorial as you follow along, so that you can actually learn and understand how they work. And if something doesn’t work as expected for you, feel free to post your queries by using the feed at the end of this tutorial. How to Create and Open a File In this section, we’ll see how to create and open a file. When it comes to creating a file, it’s the fopen function which you’ll end up using most of the time. It may seem a bit confusing to use the fopen function to create a file. In fact, the fopen function does two things: it creates a file if it doesn’t exist and also opens it for reading or writing. Let’s go through the following example to understand how it works. In the above example, the fopen function will check if the /home/tutsplus/files/tmp.txt file exists, and if it exists, it’ll open it for writing. By supplying ‘w’ in the second argument, we specify that we will be writing to the file. If the file doesn’t exist, it’ll be created right away. It’s important to note here that the /home/tutsplus/files/ directory in the above example must be writable by the web server user for the fopen function to be able to create a file. The first argument of the fopen function is the filename which you want to open. In the above example, we’ve supplied the /home/tutsplus/files/tmp.txt filename in the first argument. Again, it’s important to note that we’ve supplied an absolute path name. The second argument is the mode, which specifies the type of access you require to the opened file. The fopen function provides different modes you can choose from. For example: use the r mode to open a file for reading the r mode for both reading and writing a mode for reading and appending In our example, we’ve used the w mode, which opens the /home/tutsplus/files/tmp.txt file for writing only. Feel free to go through the official documentation to see different modes supported by the fopen function. fopen returns a file system pointer, which is used for the other file functions like reading and writing. » Read More
Like to keep reading?
This article first appeared on code.tutsplus.com. If you'd like to keep reading, follow the white rabbit.