Siksha Sarovar

Siksha Sarovar (sikshasarovar.com) is a free educational web application that helps students in India learn programming and prepare for academic and competitive exams. The platform offers structured coding courses (C, C++, Python, Java, HTML, CSS, PHP, Power BI, AI, Machine Learning, Data Science), complete university curriculum notes for BCA/MCA students with previous year question papers, Class 10 and Class 12 CBSE/HBSE school notes, and dedicated preparation material for SSC, UPSC, Banking, Railway and other government exams. Browsing the site is completely free and requires no account. Users may optionally sign in with Google solely to save their learning progress, quiz scores and personal preferences across devices.

Privacy Policy | Terms of Service | Contact Siksha Sarovar | About Siksha Sarovar

v4.0.9 · PWA
Siksha Sarovar logo
Siksha Sarovar
Your Learning Universe

Siksha Sarovar is a free e-learning platform for coding courses, BCA university notes and competitive exam preparation. Optional Google sign-in saves your learning progress across devices.

Initializing knowledge base…
Compiling modules 0%

25. File System & Disk Operations

Lesson 22 of 29 in the free PHP Programming notes on Siksha Sarovar, written by Rohit Jangra.

Working with Files and Operating System in PHP

PHP provides built-in functions to interact with the file system, directories, and disk resources. These features are widely used in logs, uploads, backups, and system utilities.

File Functions in PHP

File functions allow PHP to create, read, write, update, and delete files.

Common File Functions

FunctionPurpose
fopen()Open file
fread()Read file
fwrite()Write file
fclose()Close file
file_exists()Check file
unlink()Delete file
filesize()File size
file_get_contents()Read entire file

---

Opening Files

fopen() Function

$file = fopen("test.txt", "r");

File Modes

ModeMeaning
rRead only
wWrite (overwrite)
aAppend
r+Read + Write
w+Read + Write (overwrite)
Exam Tip: Always close a file after use.

---

Reading Files

$file = fopen("test.txt", "r");
echo fread($file, filesize("test.txt"));
fclose($file);

---

Creating & Writing Files

$file = fopen("data.txt", "w");
fwrite($file, "Welcome to PHP File Handling");
fclose($file);
Exam Line: If file does not exist, fopen() creates it in write mode.

---

Deleting Files

unlink("data.txt");
Exam Point: unlink() permanently deletes a file.

---

Creating Directories

mkdir() Function

mkdir("uploads");

Create Nested Directory

mkdir("files/images", 0777, true);

---

Manipulating Directories

FunctionPurpose
rmdir()Remove directory
is_dir()Check directory
scandir()List files
chdir()Change directory

Example: List Files in Directory

$files = scandir("uploads");
print_r($files);

---

Information about Hard Disk

PHP provides disk-related functions to get storage information.

FunctionDescription
disk_total_space()Total disk space
disk_free_space()Free disk space

Example

echo disk_total_space("C:");
echo disk_free_space("C:");
Exam Line: Disk functions return size in bytes.

---

Directory Functions

Directory functions manage folders and file navigation.

FunctionPurpose
opendir()Open directory
readdir()Read directory
closedir()Close directory

Example

$dir = opendir("uploads");
while($file = readdir($dir)){
   echo $file;
}
closedir($dir);

---

Calculating File, Directory and Disk Sizes

File Size

echo filesize("test.txt");

Directory Size (Logic Based)

$size = 0;
foreach(scandir("uploads") as $file){
   if(is_file("uploads/".$file)){
      $size += filesize("uploads/".$file);
   }
}
echo $size;

Disk Size

echo disk_total_space("/");
Exam Tip: Directory size is calculated by summing file sizes.