0% found this document useful (0 votes)
10 views

PHP FILE Handling

Uploaded by

abeerajaved559
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

PHP FILE Handling

Uploaded by

abeerajaved559
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 14

PHP FILE Handling

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 Manipulating Files


PHP has several functions for creating, reading, uploading, and editing files.
PHP readfile() Function

• The readfile() function reads a file and writes it to the output buffer.

• Assume we have a text file called "webdictionary.txt", stored on the server, that looks like
this:

• AJAX = Asynchronous JavaScript and XML


• CSS = Cascading Style Sheets
• HTML = Hyper Text Markup Language
• PHP = PHP Hypertext Preprocessor
• SQL = Structured Query Language
• SVG = Scalable Vector Graphics
• XML = EXtensible Markup Language
• 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):

• The readfile() function is useful if all you want to do is


open up a file and read its contents.
PHP File Open/Read/Close

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.
We will use the text file, "webdictionary.txt", during the lessons:
AJAX = Asynchronous JavaScript and XML
CSS = Cascading Style Sheets
HTML = Hyper Text Markup Language
PHP = PHP Hypertext Preprocessor
SQL = Structured Query Language
SVG = Scalable Vector Graphics
XML = EXtensible Markup Language

• 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. The
following example also generates a message if the fopen() function is unable
to open the specified file:
PHP Open File - fopen()Example
PHP Read File - fread()

The fread() function reads from an open file.


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.
The following PHP code reads the "webdictionary.txt" file to the end:
PHP Close File - fclose()

• The fclose() function is used to close an open file.


PHP Read Single Line - fgets()

• The fgets() function is used to read a single line from a file.


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.

• The example below reads the "webdictionary.txt" file line by line, until end-of-file is reached:
PHP Read Single Character - fgetc()

• The fgetc() function is used to read a single character from a file.

• The example below reads the "webdictionary.txt" file character by character, until end-
of-file is reached:
PHP File Create/Write
• PHP Create File - fopen()
• The fopen() function is also used to create a file. Maybe a little confusing, but in PHP,
a file is created using the same function used to open files.

• If you use fopen() on a file that does not exist, it will create it, given that the file is
opened for writing (w) or appending (a).

• The example below creates a new file called "testfile.txt". The file will be created in
the same directory where the PHP code resides:
PHP Write to File - fwrite()

• The fwrite() function is used to write to a file.

• The first parameter of fwrite() contains the name of the file to write to and the
second parameter is the string to be written.

• The example below writes a couple of names into a new file called "newfile.txt":

You might also like