PHP Files

In this lesson we will learn how to manipulate files with php.

In the begening, we must open the file.

In order to open a file in PHP we use fopen() function.

The fopen() function opens a file or URL.

Here is the syntax

int fopen(string filename, string mode [, bool use_include_path [, resource $context]])

filename - If filename is of the form "(http, https, ftp, .. )://...", it is assumed to be a URL and PHP will search for a protocol handler (also known as a wrapper) for that scheme. If wrappers aren't registered for that protocol PHP will emit a notice to help you track potential problems in your script and then continue as though filename specifies a regular file.

If PHP has decided that filename specifies a local file, then it will try to open a stream on that file. The file must be accessible to PHP, so you need to ensure that the file access permissions allow this access.

If fopen() fails, it returns FALSE and an error on failure. You can hide the error output by adding an '@' in front of the function name.

mode - specifies the mode of access to the open stream and can have the following values:

Mode Value
r Read-only. Sets the file pointer at the beginning of the file
r+ Open for Reading and Writing. Sets the file pointer at the beginning of the file
w Only open for Recording. Sets the file pointer at the beginning of the file and clears the content of file. If the file does not exist - it is trying to create.
w+ Open for Reading and Writing. Sets the file pointer at the beginning of the file and clears the content of the file. If the file does not exist - it is trying to create.
a Only open for Recording. Sets the file pointer at the end of the file. If the file does not exist - it is trying to create.
a+ Open for Reading and Writing. Sets the file pointer at the end of the file. If the file does not exist - it is trying to create.
x Only open for Recording. Sets the file pointer at the beginning of the file. If the file does not exist - it is trying to create, if file exists it returns false and error .
x+ Open for Reading and Writing. Sets the file pointer at the beginning of the file. If the file does not exist - it is trying to create, if file exists it returns false and error .

use_include_path - The optional parameter can be set to '1' or TRUE if you want the same function to search for the file in the include_path.

Now we must write in file. In order to write in file, in php we use fwrite() function .

Here is the syntax .

int fwrite ( resource handle, string string [, int length] )

Function fwrite () writes the contents of string to the file stream handle. If argument length is passed , recording will stop after length bytes have been written or reached the end of the string.

fwrite () returns the number of written bytes or FALSE on error.

And in the end we must close the file. For closing file, in php used fclose() function

Here is the syntax.

bool fclose ( resource handle )

Function fclose() closes the file pointed by handle . Returns TRUE on success or FALSE on failure.

Handle must point to a previously opened file using the fopen() function .

Let's see the example of all of this .

<?php

   $fileName = "myFile.txt";

   $file = fopen($fileName, "w+");

   fwrite($file, "Hello W3Docs !!!");

   fclose($file);

?>

In this exemple we create a file in root folder, with name "myFile.txt", with fopen() function, set the file pointer at the beginning of file with mode "w+". Then we write "Hello W3Docs !!!" to the file with function fwrite(), and close the file with fclose() function .