0% found this document useful (0 votes)
11 views10 pages

PHP Lecture No 6

The document discusses various PHP functions for handling files including reading, writing, opening, and closing files. It explains functions like readfile(), fopen(), fread(), fclose(), fgets(), fgetc(), feof(), and fwrite() and provides examples of their syntax and usage.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views10 pages

PHP Lecture No 6

The document discusses various PHP functions for handling files including reading, writing, opening, and closing files. It explains functions like readfile(), fopen(), fread(), fclose(), fgets(), fgetc(), feof(), and fwrite() and provides examples of their syntax and usage.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

File Handling Lecture No 6

MUHAMMAD AT TIQ UR REHMAN


PHP File Handling
File handling is an important part of any web application. You often need to open and process a
file for different tasks
PHP has several functions for creating, reading, uploading, and editing files.
◦ Readfile()
◦ Fopen()
◦ Fread()
◦ Fclose()
◦ Fgets()
◦ Fgetc()
◦ Feof()
◦ Fwrite()
◦ Move_upload_file()
PHP readfile() Function
The readfile() function reads a file and writes it to the output buffer.
Assume we have a text file called “abc.txt", stored on the server.
The PHP code to read the file and write it to the output buffer is as follows (the readfile()
function returns the number of bytes read on success)
Syntax
echo readfile(“abc.txt");
PHP Open File - fopen()
A better method to open files is with the fopen() function. This function gives you more options
than the readfile() function.
Syntax
fopen(“abc.txt", "r");
The first parameter of fopen() contains the name of the file to be opened and the second
parameter specifies in which mode the file should be opened.
PHP Read File - fread()
After open text file the fread() function read file from an open file.
Syntax
fread($myfile,filesize(“abc.txt"));
The first parameter of fread() contains the name of the file to read from and the second
parameter specifies the maximum number of bytes to read.
PHP Close File - fclose()
The fclose() function is used to close an open file.
It's a good programming practice to close all files after you have finished with them. You don't
want an open file running around on your server taking up resources!
Syntax
fclose ($myfile);
The fclose() requires the name of the file (or a variable that holds the filename) we want to
close.
PHP Read Single Line - fgets()
The fgets() function is used to read a single line from a file.
Syntax
fgets($openedfile);

After a call to the fgets() function, the file pointer has moved to the next line.
PHP Check End-Of-File - feof()
The feof() function checks if the "end-of-file" (EOF) has been reached.
The feof() function is useful for looping through data of unknown length.
E.g
<?php
$myfile = fopen(“abc.txt", "r") or die("Unable to open file!");
// Output one line until end-of-file
while(!feof($myfile)) {
echo fgets($myfile) . "<br>";
}
fclose($myfile);
?>
PHP Read Single Character - fgetc()
The fgetc() function is used to read a single character from a file.
Syntax
fgetc($myfile);
After a call to the fgetc() function, the file pointer moves to the next character.

If you want to read complete file using fgetc function then use feof function.
PHP Write to File - fwrite()
The fwrite() function is used to write to a file.
Syntax
fwrite($myfile, $txt);
The first parameter of fwrite() contains the name of the file to write to and the second
parameter is the string to be written.

You might also like