File Handling
File Handling
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.