File handling in PHP involves working with files on the server, such as creating, reading, writing, and manipulating files with their contents. This is essential tasks on server like storing and retrieving data, generating dynamic content, and managing file uploads.
- Handling Directory
- Handling File
- Uploading a file
- Downloading a file
- File Handling Flags
- Common File Handling Functions
- Security Considerations
- Creating a Directory: using
mkdir()
function.
mkdir("new_directory");
- Renaming a Directory: using
rename()
function.
rename("old_directory", "new_directory");
- Removing a Directory: remove an empty directory using
rmdir()
function.
rmdir("empty_directory");
- Creating a File: using
fopen()
withw
write flag.
$file = fopen("new_file.txt", "w");
fwrite($file, "Hello, world!");
fclose($file);
-
File Pointer: When open a file, the handler has pointer that indicates the current position within a file.
-
File Modes: Different modes for opening a file, such as "r" for reading, "w" for writing, "a" for appending, etc.
-
Editing a File:
$file = fopen("existing_file.txt", "r+");
$contents = fread($file, filesize("existing_file.txt"));
$contents = str_replace("old_text", "new_text", $contents);
rewind($file);
fwrite($file, $contents);
fclose($file);
- Reading a File:
$file = fopen("existing_file.txt", "r");
while (!feof($file)) {
$line = fgets($file);
echo $line;
}
fclose($file);
- Replacing a File:
$file = fopen("existing_file.txt", "w");
fwrite($file, "New content");
fclose($file);
- Removing a File:
unlink("file_to_delete.txt");
To store the uploaded file in server using, move_uploaded_file()
function.
if (isset($_FILES['fileToUpload'])) {
$targetDir = "uploads/";
$targetFile = $targetDir . basename($_FILES["fileToUpload"]["name"]);
move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $targetFile);
}
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="' . basename($file) . '"');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
readfile($file);
exit;
These flags are passed as the second argument to the fopen()
function.
r
: Open a file for reading only.w
: Open a file for writing only. If the file doesn't exist, it creates a new one. If it exists, it truncates its contents.a
: Open a file for appending. If the file doesn't exist, it creates a new one.x
: Create a new file for exclusive access. If the file already exists, the function fails.r+
: Open a file for reading and writing.w+
: Open a file for reading and writing. If the file doesn't exist, it creates a new one. If it exists, it truncates its contents.a+
: Open a file for reading and appending.b
: Open the file in binary mode.t
: Open the file in text mode (default).FILE_USE_INCLUDE_PATH
: Search for the file in the include path.FILE_IGNORE_NEW_LINES
: Omit newlines at the end of each array element when reading lines.FILE_SKIP_EMPTY_LINES
: Skip empty lines when reading lines.
Example:
// Open a file for reading
$handle = fopen("myfile.txt", "r");
// Open a file for writing, creating it if it doesn't exist
$handle = fopen("newfile.txt", "w");
// Open a file for appending, creating it if it doesn't exist
$handle = fopen("log.txt", "a");
fopen()
: Opens a file and returns a file pointer.$handle = fopen("myfile.txt", "r"); // Opens a file for reading
fclose()
: Closes an open file.fclose($handle);
fread()
: Reads a specified number of bytes from a file.$data = fread($handle, 1024);
fgets()
: Reads a line from a file.$line = fgets($handle);
file_get_contents()
: Reads an entire file into a string.$contents = file_get_contents("myfile.txt");
fwrite()
: Writes data to a file.fwrite($handle, "Hello, world!");
file_put_contents()
: Writes data to a file.file_put_contents("myfile.txt", "Hello, world!");
file_exists()
: Checks if a file exists.if (file_exists("myfile.txt")) { // File exists }
filesize()
: Gets the size of a file in bytes.$size = filesize("myfile.txt");
unlink()
: Deletes a file.unlink("myfile.txt");
is_dir()
:Checks if a file is a directory.$filename = 'myfile.txt'; is_dir($filename);
is_file()
:Checks if a file is a regular file.$filename = 'myfile.txt'; is_file($filename);
mkdir()
: Creates a directory.mkdir("new_directory");
rmdir()
: Deletes a directory.rmdir("old_directory");
copy()
: Copies a file.copy($sourcefile, $destination);
scandir()
: Lists files and directories in a directory.scandir("directory_name");
flock()
: Acquires an exclusive lock on a file.flock($handle, 'w');
feof()
: Checks if the end of a file has been reached.feof($handle);
rewind()
: Rewinds the position of the file pointer to the beginning.rewind($handle);
- Validate and sanitize files: files, names and paths are sanitized to prevent security vulnerabilities like directory traversal attacks.
- Secure File Uploads: Validate files with their types, sizes, and destinations to prevent malicious uploads.
- File Permissions: Set appropriate file permissions to restrict access to sensitive files.
- Error Handling: Implement error handling to prevent information disclosure and security vulnerabilities.
- Regular Security Audits: Conduct regular security audits to identify and fix malicious files and vulnerabilities.