PHP working with files. Open file

PHP provides real comfort when you are working with files. It does not matter whether they are on the local server ‘localhost’ or on a remote server. There are many cases when you will have to work with the content of different files, for example when you are using simple counters or string treatment of any kind. As well as many other abilities of the language the work with files in PHP is easy and simple.
We will provide the main functions and constructions which will help you to perform operations on the file server as well as work with remote files.

Opening file in PHP.

Before doing anything with any file the first step is to open it. We can do that with ‘fopen ()’ function.

As you can see the ‘fopen ()’ function accept two parameters ‘“ the filename (”/www/file.php”) and the condition of opening ("r") which stands for reed only. We can put the full path (server/user/plan/root_folder/www/file.*) or the relative path (/www/file.*) if it is located on the local server, or to describe the URL if it is on e remote server in Internet. As we mentioned the parameter ‘r’ is for setting the condition of the file when it is opened. Here are the different variations of this parameter:

To describe how the file functions are working we will give you the notion here.
When ‘fopen ()’ function is executed it will return the file identification variable(in this case ‘$fp’), which variable we will use every time when we are working with that open file. Besides that there is so called pointer which simply is showing where we are going to do any changed on the file.
For example if we want to open a file to put some text lines inside using the parameter ‘a+’ the editing will be at the end of the file. And if we use the parameter ‘w+’ the editing will start at the beginning of the file. You should do difference between ‘a+’ and ‘а’ since the second will open the file for writing only.

We will continue with the next PHP tutorial about reading a file in PHP.

Filed Under: PHP

Anthony Gee About the Author: Anthony G. is an IT specialist with more than 9 years of solid working experience in the Web Hosting industry. Currently works as server support administrator, involved in consultative discussions about Web Hosting and server administration. One of the first writers in the Onlinehowto.net website, now writing for Free Tutorials community - he is publishing tutorials and articles for the wide public, as well as specific technical solutions.

Leave a Reply