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

File Handling

Uploaded by

ssascw.bca
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

File Handling

Uploaded by

ssascw.bca
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

File Handling in PHP

File handling in PHP involves working with files


to read, write, and manage their content. PHP
provides various functions to handle files
efficiently.
1. File Operations
a. Reading Files
Reading files in PHP allows you to access and
use the data stored within them. You can read
files line by line or in chunks.
• fread(): Reads from an open file.
• file_get_contents(): Reads the entire file into
a string.
Example:
b. Writing Files
Writing to files allows you to store data into
files. You can either overwrite the existing
content or append new data.
• fwrite(): Writes data to an open file.
• file_put_contents(): Writes data to a file (can
overwrite or append).
Example:
c. File Pointers
File pointers keep track of the current position
in the file while reading or writing.
• ftell(): Returns the current position of the file
pointer.
• fseek(): Moves the file pointer to a specified
location.
• rewind(): Moves the file pointer to the
beginning of the file.
Example:
d. File Upload Handling
File upload handling in PHP allows you to
handle files submitted through HTML forms.
• $_FILES: A superglobal array that contains
information about the uploaded files.
Example:

2. File Functions
a. fopen()
• Opens a file for reading, writing, or both.
• Syntax: fopen(filename, mode)
Modes:
• "r": Read-only.
• "w": Write-only, truncates the file or creates a
new one.
• "a": Write-only, appends to the file or creates
a new one.
• "r+", "w+", "a+": Read/write combinations.
Example:

b. fread()
• Reads from an open file.
• Syntax: fread(file, length)
Example:
c. fwrite()
• Writes to an open file.
• Syntax: fwrite(file, string)
Example:

d. fclose()
• Closes an open file.
• Syntax: fclose(file)
Example:

e. file_get_contents()
• Reads the entire file into a string.
• Syntax: file_get_contents(filename)
Example:

f. file_put_contents()
• Writes a string to a file.
• Syntax: file_put_contents(filename, data)
Example:

Summary
• File Operations include reading, writing,
managing file pointers, and handling file
uploads.
• File Functions such as fopen, fread, fwrite,
fclose, file_get_contents, and
file_put_contents provide the core
functionality for file handling in PHP.

You might also like