Professional Documents
Culture Documents
Prepared by:
ENGR. GIL POLIGRATES
OBJECTIVE
Before doing any activity with files you need to open the
file, also when you are going to open file then you can
set mode, in which you want to open. Mode will be like
read, write etc.
Modes Descriptions
r, r+, w, w+, a, a+, x, x+
OPENING & CLOSING A FILE
Modes Descriptions
r Read only. Pointers at the beginning of the file
r+ Read and Write both. Pointers at the beginning of the
file
w Write only. Opens and clears the contents of file; or
creates a new file if it doesn’t exist
w+ Read and Write both. Opens and clears the contents
of file or creates a new file if it doesn’t exist
OPENING & CLOSING A FILE
Modes Descriptions
a Append. Opens and writes to the end of the file or
creates a new file if it doesn’t exist
a+ Read and Append both. Preserves file content by
writing to the end of the file
x Write only. Creates a new file. Returns FALSE and an
error if file already exists
x+ Read and Write both. Creates a new file. Returns
FALSE and an error if file already exists
OPEN A FILE
Example:
$my_file = 'myfile.txt';
$handle = fopen($my_file, 'w') or die('Cannot open file:
'.$my_file);
READ A FILE
Example:
$my_file = 'myfile.txt';
$handle = fopen($my_file, 'r');
$data = fread($handle,filesize($my_file));
WRITE TO A FILE
Example:
$my_file = 'myfile.txt';
$handle = fopen($my_file, 'w') or die('Cannot open file:
'.$my_file);
$data = 'This is the data which you have to write in your
file';
fwrite($handle, $data);
CLOSE A FILE
Example:
$my_file = 'myfile.txt';
$handle = fopen($my_file, 'w') or die('Cannot open file:
'.$my_file);
//write some data here
fclose($handle);
DELETE A FILE
Example:
$my_file = 'myfile.txt';
unlink($my_file);
READING A FILE LINE BY LINE
The fgets() function is used to read a single line from a
file and then file pointer is pointing to the next line in the
file. so by this way we can read file line by line.
Example:
$file = fopen("myfile.txt", "r") or exit("Unable to open
the file!");
while(!feof($file)){
echo fgets($file). "<br>";
}
fclose($file);
READING A FILE LINE BY LINE
</body>
</html>
CREATING AN UPLOAD SCRIPT
There is one global PHP variable called $_FILES. This
variable is an associate double dimension array and
keeps all the information related to uploaded file.