Working with files and directories is one of the most important tasks in PHP. Whether you are uploading files, reading configuration data, generating reports, creating backups, or managing logs—PHP provides powerful functions to handle everything.
This guide explains how files and folders work, how PHP interacts with them, and real-world use cases, all in a clear and practical way.
⭐ What Are Files & Directories in PHP?
Files
A file stores data such as:
- Text
- JSON
- Images
- PDFs
- Logs
- Configurations
PHP can create, read, write, delete, move, and upload files easily.
Directories (Folders)
Directories help you organize files. PHP can:
- Create folders
- Scan directories
- List files
- Delete directories
- Move files to other folders
Understanding PHP’s file and directory handling helps you build powerful applications.
⭐ Understanding File Paths in PHP
PHP works with two types of paths:
1. Absolute Path
Full path from your server root.
Example:/var/www/html/project/images/logo.png
or on Windows:C:/xampp/htdocs/project/data.txt
2. Relative Path
Starts from the current file location.
Examples:
images/logo.png
../config/settings.php
Relative paths are common in real projects.
⭐ Checking If a File Exists
if (file_exists("data.txt")) {
echo "File found!";
} else {
echo "File not found.";
}
Useful for preventing errors.
⭐ Reading a File in PHP
1. Quick Read (One Line)
$content = file_get_contents("data.txt");
echo $content;
2. Traditional Read Method
$handle = fopen("data.txt", "r");
echo fread($handle, filesize("data.txt"));
fclose($handle);
⭐ Writing to a File
Rewrite the file
file_put_contents("log.txt", "New log entry");
Append data to a file
file_put_contents("log.txt", "New entry\n", FILE_APPEND);
Used for:
- Logs
- Form submissions
- Storing temporary data
⭐ Creating a New File
$handle = fopen("newfile.txt", "w");
fwrite($handle, "Hello World!");
fclose($handle);
⭐ Deleting a File
unlink("oldfile.txt");
⭐ Working with Directories
Create a Directory
mkdir("uploads");
Check if Directory Exists
if (is_dir("uploads")) {
echo "Directory exists!";
}
Remove a Directory
rmdir("oldfolder");
(Folder must be empty.)
⭐ Scanning a Directory (List All Files)
$files = scandir("uploads");
print_r($files);
Output example:
Array
(
[0] => .
[1] => ..
[2] => file1.jpg
[3] => file2.png
)
Useful for:
- File managers
- Image galleries
- Admin dashboards
⭐ Moving / Renaming Files
rename("oldname.txt", "newname.txt");
Or to move into another folder:
rename("file.txt", "archive/file.txt");
⭐ Getting File Information
echo filesize("data.txt"); // File size
echo filetype("data.txt"); // File type
⭐ Real-World Applications
✔ File upload systems
✔ Image gallery
✔ Log files for debugging
✔ Creating backups
✔ Reading JSON/XML files
✔ Managing folders for each user
✔ Generating printable reports
⭐ Best Practices
✔ Always check if files exist before accessing them
✔ Use correct permissions (read, write, execute)
✔ Never trust uploaded files — validate them
✔ Avoid using absolute paths when possible
✔ Close file handles to avoid memory leaks
✔ Use folders to organize and separate logic
Citations
🔗 View other articles about PHP:
http://savanka.com/category/learn/php/
🔗 External PHP Documentation:
https://www.php.net/manual/en/