File Close
The next step after you have opened a file is to close that file.
Description
In PHP it is not required to close all your files after using them because the server will close all files after the PHP code finishes execution.
File Close Function
In a previous section, we had a call to the function fclose to close down a file after we were done with it.
PHP Code
$fname = “first.txt”;
$handle = fopen($fname, ‘w’) or die(”can’t open file”);
fclose($handle);
The function fclose requires the file handle that we want to close. In our example we set our variable “$fileHandle” equal to the file handle returned by the fopen function.
After a file has been closed with fclose it is impossible to read, write or append to that file unless it is once more opened up with the fopen function.
File Write
Lets get on to the most useful part of file manipulation, writing. There is really only one main function that is used to write and it’s logically called fwrite.
File Open : Write
Before we can write information to our test file we have to use the function fopen to open the file for writing.
PHP Code:
$fname = “first.txt”;
$f = fopen($fname, ‘w’);
PHP - File Write: fwrite Function
We can use php to write to a text file. The fwrite function allows data to be written to any type of file. Fwrite’s first parameter is the file handle and its second parameter is the string of data that is to be written. Just give the function those two bits of information and you’re good to go!
Below we are writing a couple of names into our test file first.txt and separating them with a carriage return.
PHP Code:
$fname = “first.txt”;
$f = fopen($fname, ‘w’) or die(”can’t open file”);
$data = “Php tutorial site”;
fwrite($f, $data);
Php tutorial site is an outstanding source for learning PHP.
$data = “is an outstanding source for learning PHP.”;
fwrite($f, $data);
fclose($f);
The $f variable contains the file handle for first.txt. The file handle knows the current file pointer, which for writing, starts out at the beginning of the file.
We wrote to the file first.txt twice. Each time we wrote to the file we sent the string $data that first contained different string information. After we finished writing we closed the file using the fclose function.
If you were to open the first.txt file in NOTEPAD or wordpad it would look like this:
Php tutorial site is an outstanding source for learning PHP.
File Write: Overwriting
Now that first.txt contains some data ,when you open an existing file for writing, all the data contained in the file is erased and you start with an empty file. In this example we open our existing file first.txt and write some new data into it.
PHP Code:
$fname = “first.txt”;
$f = fopen($fname, ‘w’) or die(”can’t open file”);
$data = “Welcome”;
fwrite($f, $data);
$data = “to the world of PHP”;
fwrite($f, $data);
fclose($f)
If you now open the first.txt file you will see that previous data is erased and only the data we just wrote is there.
Contents of the first.txt File:
Welcome to the world of PHP
File Read
How to get information from files? In this lesson we will teach you how to read data from a file using various PHP functions.
File Open:Read
Before we can read information from a file we have to use the function fopen to open the file for reading. Here’s the code to read-open the file we created in the PHP File Write lessons.
PHP Code:
$fname = “first.txt”;
$f = fopen($fname, ‘r’);
The file we created in the last lesson was named “first.txt”.
first.txt Contents:
Welcome to the world of PHP
Now that the file is open, with read permissions enabled, we can get started!
FREAD Function
The fread function is used for getting data out of a file. The function requires a file handle, which we have, and an integer to tell the function how much data, in bytes, it is supposed to read.
One character is equal to one byte. If you wanted to read the first five characters then you would use five as the integer.
Code
$fname = “first.txt”;
$f = fopen($fname, ‘r’);
$data = fread($f, 7);
fclose($f);
echo $data;
Display:
Welcome
The first seven characters from the first.txt file are now stored inside $data. You could echo this string, $data.
If you wanted to read all the data from the file, then you need to get the size of the file. The filesize function returns the length of a file, in bytes, which is just what we need! The filesize function requires the name of the file that is to be sized up.
$fname = “first.txt”;
$f = fopen($fname, ‘r’);
$data = fread($f, filesize($fname));
fclose($f);
echo $data;
Display:
Welcome to the world of PHP
PHP - File Read: gets Function
PHP also lets you read a line of data at a time from a file with the gets function.
Suppose content of your new.txt file is.
Hello
Welcome to the world of PHP
We are learning file functions.
Code
$fname = “new.txt”;
$f = fopen($fname, ‘r’);
$data = fgets($f);
fclose($f);
echo $data;
The fgets function searches for the first occurrence of “\n” the newline character.
Display
Hello
Welcome to the world of PHP
We are learning file functions.
File Delete
Now you know how to create a file. Now we will learn how to delete file. In PHP you delete files by calling the unlink function. If you unlink a file, you are deleting it. Before you can unlink a file, you must first be sure that it is not open in your program. Use the fclose function to close down an open file.
Unlink Function
PHP Code:
$fname = “first.txt”;
$f = fopen($fname, ‘w’) or die(”can’t open file”);
fclose($f);
Now to delete first.txt we simply run a PHP script that is located in the same directory. Unlink just needs to know the name of the file.
$fname = “first.txt”;
unlink($fname);
The first.txt should now be removed.
File Append
We have learned how to open, close, read, and write to a file. If you want to append to a file, that is, add on to the existing data, then you need to open the file in append mode.
File Open: Append
PHP Code:
$fname = “first.txt”;
$f = fopen($fname, ‘a’);
If we were to write to the file it would begin writing data at the end of the file.
PHP - File Write: Appending Data
Using the first.txt file we created in the previous lessons, we are going to append on some more data.
PHP Code:
$fname = “first.txt”;
$f = fopen($fname, ‘a’) or die(”can’t open file”);
$data = “Appending Data”;
fwrite($f, $data);
$data = “To the file”;
fwrite($f, $data);
fclose($f);
The contents of the file first.txt would now look like this (assuming that content of file previously was “Welcome to the world of php”
Contents of the first.txt File:
Welcome to the world of php
Appending data to the file


