SlideShare a Scribd company logo
PHP File
PHP File Handling
The fopen() function is used to open files in PHP.
Opening a File
The fopen() function is used to open files in PHP.
The first parameter of this function contains the name of the
file to be opened and the second parameter specifies in
which mode the file should be opened:
<html>
<body>
<?php
$file=fopen("welcome.txt","r");
?>
</body>
</html>
The file may be opened in one of the
following modes:
Modes Description
r Read only. Starts at the beginning of the file
r+ Read/Write. Starts 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/Write. Opens and clears the contents of file; or creates a
new file if it doesn't exist
a Append. Opens and writes to the end of the file or creates a
new file if it doesn't exist
a+ Read/Append. 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/Write. Creates a new file. Returns FALSE and an error if
file already exists
Note: If the fopen() function is unable to open the specified file, it returns 0
(false).
Example
The following example generates a message if the
fopen() function is unable to open the specified file:
<html>
<body>
<?php
$file=fopen("welcome.txt","r") or exit("Unable to open
file!");
?>
</body>
</html>
Closing a File
The fclose() function is used to close an open file:
<?php
$file = fopen("test.txt","r");
//some code to be executed
fclose($file);
?>
Check End-of-file
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.
Note: You cannot read from files opened in w, a,
and x mode!
if (feof($file)) echo "End of file";
Reading a File Line by Line
The fgets() function is used to read a single line from a file.
Note: After a call to this function the file pointer has moved to
the next line.
Example
The example below reads a file line by line, until the end of
file is reached:
<?php
$file = fopen("welcome.txt", "r") or exit("Unable to open
file!");
//Output a line of the file until the end is reached
while(!feof($file))
{
echo fgets($file). "<br />";
}
fclose($file);
?>
Reading a File Character by
Character
The fgetc() function is used to read a single character
from a file.
Note: After a call to this function the file pointer moves
to the next character.
Example
The example below reads a file character by character,
until the end of file is reached:
<?php
$file=fopen("welcome.txt","r") or exit("Unable to open
file!");
while (!feof($file))
{
echo fgetc($file);
}
fclose($file);
?>
lecture 10.pptx

More Related Content

DOCX
Php files
PPTX
PHP Filing
PPTX
Files in php
PDF
Web Development Course: PHP lecture 3
PPT
PHP - Introduction to File Handling with PHP
DOCX
Php advance
PPSX
DIWE - File handling with PHP
PDF
PHP file handling
Php files
PHP Filing
Files in php
Web Development Course: PHP lecture 3
PHP - Introduction to File Handling with PHP
Php advance
DIWE - File handling with PHP
PHP file handling

Similar to lecture 10.pptx (20)

PPT
php file uploading
PPTX
PHP File Handling
PPT
Php File Operations
PPTX
Ch3(working with file)
PPT
Filing system in PHP
PPTX
Working of File System: An Overview in detail
DOCX
Files nts
PPTX
Php File Operations
PPT
MySQL PHP Introduction Material for Course Web Programming
PPTX
4 - Files and Directories - Pemrograman Internet Lanjut.pptx
PDF
File system
PPT
Php basics
PPTX
4-chapter-File & Directores.pptx debre CTABOUR UNIversit
PPTX
Chap 5 php files part 1
PPTX
object oriented programming in PHP & Functions
PPT
Php i basic chapter 4
PPTX
File handing in C
php file uploading
PHP File Handling
Php File Operations
Ch3(working with file)
Filing system in PHP
Working of File System: An Overview in detail
Files nts
Php File Operations
MySQL PHP Introduction Material for Course Web Programming
4 - Files and Directories - Pemrograman Internet Lanjut.pptx
File system
Php basics
4-chapter-File & Directores.pptx debre CTABOUR UNIversit
Chap 5 php files part 1
object oriented programming in PHP & Functions
Php i basic chapter 4
File handing in C
Ad

More from ITNet (20)

PDF
lecture 8 b main memory
PPTX
lecture 9.pptx
PPTX
lecture 11.pptx
PPTX
lecture 12.pptx
PPTX
lecture 13.pptx
PPTX
lecture 15.pptx
PDF
kandegeeee.pdf
PPT
Ia 124 1621324160 ia_124_lecture_02
PPT
Ia 124 1621324143 ia_124_lecture_01
PPTX
Cp 121 lecture 01
PDF
Cp 111 5 week
DOCX
Teofilo kisanji university mbeya (TEKU) ambassador 2020
PPT
Tn 110 lecture 8
PPTX
Tn 110 lecture 2 logic
PPTX
Tn 110 lecture 1 logic
PPTX
internet
PPTX
Im 111 lecture 1
PPT
development study perspective full
PPTX
Gender issues in developement
PDF
Religion
lecture 8 b main memory
lecture 9.pptx
lecture 11.pptx
lecture 12.pptx
lecture 13.pptx
lecture 15.pptx
kandegeeee.pdf
Ia 124 1621324160 ia_124_lecture_02
Ia 124 1621324143 ia_124_lecture_01
Cp 121 lecture 01
Cp 111 5 week
Teofilo kisanji university mbeya (TEKU) ambassador 2020
Tn 110 lecture 8
Tn 110 lecture 2 logic
Tn 110 lecture 1 logic
internet
Im 111 lecture 1
development study perspective full
Gender issues in developement
Religion
Ad

Recently uploaded (20)

PDF
Empathic Computing: Creating Shared Understanding
PPTX
Tartificialntelligence_presentation.pptx
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PDF
Heart disease approach using modified random forest and particle swarm optimi...
PDF
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
PPTX
SOPHOS-XG Firewall Administrator PPT.pptx
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PPTX
Group 1 Presentation -Planning and Decision Making .pptx
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PDF
MIND Revenue Release Quarter 2 2025 Press Release
PPTX
Spectroscopy.pptx food analysis technology
PPTX
cloud_computing_Infrastucture_as_cloud_p
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PDF
Network Security Unit 5.pdf for BCA BBA.
PDF
Encapsulation_ Review paper, used for researhc scholars
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PDF
Approach and Philosophy of On baking technology
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PPTX
OMC Textile Division Presentation 2021.pptx
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
Empathic Computing: Creating Shared Understanding
Tartificialntelligence_presentation.pptx
Diabetes mellitus diagnosis method based random forest with bat algorithm
Heart disease approach using modified random forest and particle swarm optimi...
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
SOPHOS-XG Firewall Administrator PPT.pptx
Agricultural_Statistics_at_a_Glance_2022_0.pdf
Group 1 Presentation -Planning and Decision Making .pptx
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
MIND Revenue Release Quarter 2 2025 Press Release
Spectroscopy.pptx food analysis technology
cloud_computing_Infrastucture_as_cloud_p
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
Network Security Unit 5.pdf for BCA BBA.
Encapsulation_ Review paper, used for researhc scholars
Mobile App Security Testing_ A Comprehensive Guide.pdf
Approach and Philosophy of On baking technology
Per capita expenditure prediction using model stacking based on satellite ima...
OMC Textile Division Presentation 2021.pptx
Advanced methodologies resolving dimensionality complications for autism neur...

lecture 10.pptx

  • 2. PHP File Handling The fopen() function is used to open files in PHP. Opening a File The fopen() function is used to open files in PHP. The first parameter of this function contains the name of the file to be opened and the second parameter specifies in which mode the file should be opened: <html> <body> <?php $file=fopen("welcome.txt","r"); ?> </body> </html>
  • 3. The file may be opened in one of the following modes: Modes Description r Read only. Starts at the beginning of the file r+ Read/Write. Starts 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/Write. Opens and clears the contents of file; or creates a new file if it doesn't exist a Append. Opens and writes to the end of the file or creates a new file if it doesn't exist a+ Read/Append. 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/Write. Creates a new file. Returns FALSE and an error if file already exists Note: If the fopen() function is unable to open the specified file, it returns 0 (false).
  • 4. Example The following example generates a message if the fopen() function is unable to open the specified file: <html> <body> <?php $file=fopen("welcome.txt","r") or exit("Unable to open file!"); ?> </body> </html>
  • 5. Closing a File The fclose() function is used to close an open file: <?php $file = fopen("test.txt","r"); //some code to be executed fclose($file); ?>
  • 6. Check End-of-file 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. Note: You cannot read from files opened in w, a, and x mode! if (feof($file)) echo "End of file";
  • 7. Reading a File Line by Line The fgets() function is used to read a single line from a file. Note: After a call to this function the file pointer has moved to the next line. Example The example below reads a file line by line, until the end of file is reached: <?php $file = fopen("welcome.txt", "r") or exit("Unable to open file!"); //Output a line of the file until the end is reached while(!feof($file)) { echo fgets($file). "<br />"; } fclose($file); ?>
  • 8. Reading a File Character by Character The fgetc() function is used to read a single character from a file. Note: After a call to this function the file pointer moves to the next character. Example The example below reads a file character by character, until the end of file is reached: <?php $file=fopen("welcome.txt","r") or exit("Unable to open file!"); while (!feof($file)) { echo fgetc($file); } fclose($file); ?>