Chapter 7
Chapter 7
Chapter 7
File Handling
7.1 Introduction to File Handling
File handling allows web applications to interact with files on the server. This is essential for tasks like
storing user data, logging events, and managing configuration settings. Without file handling, web
applications would rely solely on databases, which might not be practical for all types of data storage.
Data persistence, where files store information like user preferences between sessions
Logging, which helps web applications track user actions, errors, and events for debugging and
monitoring
Configuration management, where settings are saved in files for flexibility and easy customization
Content management, enabling websites with dynamic content, such as blogs or news sites, to store
and serve content efficiently
Examples of file handling in web development include access logs, where web servers like Apache or Nginx
store logs of each request made to the server. Error logs, where applications log errors to a file for easier
debugging. User uploads, where platforms like social media sites handle file uploads (images, documents)
and store them on the server. Configuration files, where web apps often use JSON, XML, or PHP files to
control behavior without changing the code. Data backups, where periodic backups of database exports
or critical data files ensure recovery from data loss.
PHP provides various functions to handle files efficiently. Some key functions include the following:
Opening a File
The fopen() function is used to open files. It requires two main arguments: the file name and the mode.
Example:
1 of 6
Internet Programming II - 2017EC
Mode Description
r Read-only. Starts at the beginning of the file.
r+ Read and write. Starts at the beginning of the file.
w Write-only. Erases the content or creates a new file.
w+ Read and write. Erases the content or creates a new file.
a Write-only. Appends data to the end of the file. Creates a new file if it doesn’t exist.
a+ Read and append. Starts at the end of the file. Creates a new file if it doesn’t exist.
x Write-only. Creates a new file. Returns FALSE if the file already exists.
x+ Read and write. Creates a new file. Returns FALSE if the file already exists.
c Write-only. Opens the file but does not create it if it doesn’t exist. Doesn’t overwrite content.
c+ Read and write. Similar to c, but allows reading too.
fgets() Reads the file line-by-line (until newline \n), useful for processing large files without loading
the entire content
end.
2 of 6
Internet Programming II - 2017EC
Writing to Files
It’s essential to handle errors, like when a file doesn’t exist or lacks permissions.
Example:
Permissions: Ensure proper permissions (e.g., chmod 644) to prevent unauthorized access.
Security: Avoid reading/writing sensitive files like /etc/passwd. Always validate file paths.
Simple Log System (build a basic log system to track user visits)
3 of 6
Internet Programming II - 2017EC
Deleting Files
$profilePic = "uploads/user123.jpg";
if (file_exists($profilePic)) {
unlink($profilePic);
echo "Profile picture deleted.";
} else {
echo "File not found.";
}
Creating Directories
$username = "user123";
$userFolder = "uploads/$username";
if (!is_dir($userFolder)) {
mkdir($userFolder, 0755, true);
echo "User folder created.";
} else {
echo "Folder already exists.";
}
0755 is the permission setting, and true allows nested folders if needed.
4 of 6
Internet Programming II - 2017EC
Removing Directories
$categoryFolder = "uploads/category-abc";
$_SERVER['DOCUMENT_ROOT']gives the root directory of the server where the current script is running.
include(__DIR__ . "/config.php");
Resizing Images
5 of 6
Internet Programming II - 2017EC
$image = imagecreatefromjpeg($file);
6 of 6