Topic: PHP Basics – Part 8 of 10: Working with Files and Directories
---
1. Introduction to File Handling in PHP
• PHP allows you to create, read, write, append, and delete files on the server.
• You can also manage directories, check if a file exists, and more.
---
2. Opening a File
Use the
•
| Mode | Description |
| ------ | -------------------------------- |
|
|
|
|
|
---
3. Reading from a File
•
• Always use
---
4. Writing to a File
• If the file doesn't exist, it will be created.
• If it exists, it will be overwritten.
---
5. Appending to a File
•
---
6. Reading Line by Line
•
•
---
7. Checking If File Exists
---
8. Deleting a File
---
9. Working with Directories
• Create a directory:
• Check if a directory exists:
• Delete a directory:
---
10. Scanning a Directory
• Returns an array of file and directory names.
---
11. Uploading Files
This is a common use case when working with files in PHP.
HTML Form:
upload.php:
---
12. Summary
• PHP provides powerful tools for file and directory operations.
• You can manage content, upload files, read/write dynamically, and handle directories with ease.
---
Exercise
• Create a PHP script that:
* Checks if a file named
* If it does, reads and prints its contents
* If not, creates the file and writes a welcome message
---
#PHP #FileHandling #Directories #PHPTutorial #BackendDevelopment
https://t.iss.one/Ebooks2023
---
1. Introduction to File Handling in PHP
• PHP allows you to create, read, write, append, and delete files on the server.
• You can also manage directories, check if a file exists, and more.
---
2. Opening a File
Use the
fopen() function:$handle = fopen("example.txt", "r");•
"r" means read-only. Other modes include:| Mode | Description |
| ------ | -------------------------------- |
|
"r" | Read-only ||
"w" | Write-only (truncates file) ||
"a" | Append ||
"x" | Create & write (fails if exists) ||
"r+" | Read & write |---
3. Reading from a File
$handle = fopen("example.txt", "r");
$content = fread($handle, filesize("example.txt"));
fclose($handle);
echo $content;•
fread() reads the entire file based on its size.• Always use
fclose() to release system resources.---
4. Writing to a File
$handle = fopen("newfile.txt", "w");
fwrite($handle, "Hello from PHP file writing!");
fclose($handle);• If the file doesn't exist, it will be created.
• If it exists, it will be overwritten.
---
5. Appending to a File
$handle = fopen("log.txt", "a");
fwrite($handle, "New log entry\n");
fclose($handle);•
"a" keeps existing content and adds to the end.---
6. Reading Line by Line
$handle = fopen("example.txt", "r");
while (!feof($handle)) {
$line = fgets($handle);
echo $line . "<br>";
}
fclose($handle);•
feof() checks for end of file.•
fgets() reads a single line.---
7. Checking If File Exists
if (file_exists("example.txt")) {
echo "File found!";
} else {
echo "File not found!";
}---
8. Deleting a File
if (file_exists("delete_me.txt")) {
unlink("delete_me.txt");
echo "File deleted.";
}---
9. Working with Directories
• Create a directory:
mkdir("myfolder");• Check if a directory exists:
if (is_dir("myfolder")) {
echo "Directory exists!";
}• Delete a directory:
rmdir("myfolder"); // Only works if empty---
10. Scanning a Directory
$files = scandir("myfolder");
print_r($files);• Returns an array of file and directory names.
---
11. Uploading Files
This is a common use case when working with files in PHP.
HTML Form:
<form action="upload.php" method="post" enctype="multipart/form-data">
<input type="file" name="uploadedFile">
<input type="submit" value="Upload">
</form>
upload.php:
if ($_FILES["uploadedFile"]["error"] === 0) {
$target = "uploads/" . basename($_FILES["uploadedFile"]["name"]);
move_uploaded_file($_FILES["uploadedFile"]["tmp_name"], $target);
echo "Upload successful!";
}---
12. Summary
• PHP provides powerful tools for file and directory operations.
• You can manage content, upload files, read/write dynamically, and handle directories with ease.
---
Exercise
• Create a PHP script that:
* Checks if a file named
data.txt exists* If it does, reads and prints its contents
* If not, creates the file and writes a welcome message
---
#PHP #FileHandling #Directories #PHPTutorial #BackendDevelopment
https://t.iss.one/Ebooks2023
❤2