Lecture Notes On Class 13: File Handling - Part 2

Rashmi Mishra
0

Lecture Notes On Class 13: File Handling - Part 2


Objective:

  • Explore advanced file handling techniques, including working with directories.
  • Learn to manage file paths and directory operations.

Outcome:

  • Students will be able to create, delete, and list files and directories and handle file paths effectively.

Introduction to File Handling

In the previous class, we covered basic file handling techniques in PHP. We learned how to open, read, write, and close files. In this class, we will build on that foundation by exploring more advanced file handling techniques, focusing on directories and file paths.


1. Understanding Directories

A directory is a container for files. In PHP, we can create, delete, and manipulate directories using built-in functions. Managing directories is essential for organizing files in a structured manner.

1.1 Common Directory Operations

  • Creating a Directory: To create a new directory, we use the mkdir() function.
  • Deleting a Directory: To remove a directory, we use the rmdir() function. Note that the directory must be empty.
  • Listing Files and Directories: To retrieve a list of files and directories, we use the scandir() function.

2. Working with Directories

2.1 Creating a Directory

To create a new directory, we use the mkdir() function. The syntax is as follows:

php

Copy code

mkdir("directory_name", mode);

  • Parameters:
    • directory_name: The name of the directory you want to create.
    • mode (optional): The permissions for the directory. This is typically set to 0777, allowing read, write, and execute permissions.

Example:

php

Copy code

<?php

$dir = "new_folder";

 

if (!is_dir($dir)) {  // Check if directory already exists

    mkdir($dir, 0777);

    echo "Directory created successfully!";

} else {

    echo "Directory already exists.";

}

?>

2.2 Deleting a Directory

To delete a directory, use the rmdir() function. The syntax is as follows:

php

Copy code

rmdir("directory_name");

Example:

php

Copy code

<?php

$dir = "new_folder";

 

if (is_dir($dir)) {

    rmdir($dir);

    echo "Directory deleted successfully!";

} else {

    echo "Directory does not exist.";

}

?>

Note: You must ensure that the directory is empty before you can delete it.

2.3 Listing Files and Directories

To retrieve a list of files and directories within a specified directory, we use the scandir() function. This function returns an array of files and directories.

Syntax:

php

Copy code

$files = scandir("directory_name");

Example:

php

Copy code

<?php

$dir = "."; // Current directory

 

$files = scandir($dir);

 

echo "Files and directories in '$dir':<br>";

foreach ($files as $file) {

    if ($file != "." && $file != "..") { // Skip current and parent directory references

        echo $file . "<br>";

    }

}

?>


3. Managing File Paths

Understanding file paths is crucial for file handling. File paths can be absolute or relative:

  • Absolute Path: Specifies the full path to the file or directory, starting from the root directory. Example: /var/www/html/file.txt
  • Relative Path: Specifies the path relative to the current directory. Example: ./file.txt or ../folder/file.txt

3.1 Getting the Current Working Directory

You can retrieve the current working directory using the getcwd() function.

Example:

php

Copy code

<?php

$current_dir = getcwd();

echo "Current Directory: " . $current_dir;

?>

3.2 Changing the Current Directory

To change the current working directory, use the chdir() function.

Example:

php

Copy code

<?php

chdir("/path/to/directory");

echo "Changed Directory to: " . getcwd();

?>


4. Practical Applications

4.1 Creating and Organizing Project Directories

Let’s say we are working on a project that requires separate directories for images, documents, and backups. We can automate the creation of these directories:

php

Copy code

<?php

$directories = ['images', 'docs', 'backups'];

 

foreach ($directories as $dir) {

    if (!is_dir($dir)) {

        mkdir($dir, 0777);

        echo "Directory '$dir' created successfully!<br>";

    }

}

?>

4.2 Deleting Old Directories

To clean up old directories, you could write a script that deletes directories that have not been modified for a certain period:

php

Copy code

<?php

$dir = "old_folder";

 

if (is_dir($dir)) {

    if (rmdir($dir)) {

        echo "Directory '$dir' deleted successfully!";

    } else {

        echo "Directory '$dir' could not be deleted. It might not be empty.";

    }

}

?>


Summary

In this class, we covered advanced file handling techniques, including working with directories and managing file paths. You learned how to create, delete, and list files and directories. Additionally, we explored how to handle file paths effectively.

Homework/Practice Assignment

  1. Create a PHP script that organizes files into different directories based on their extensions (e.g., .txt, .jpg, .pdf).
  2. Write a script that counts the number of files in a specified directory and outputs the count.

Questions and Discussion

Feel free to ask any questions you may have regarding file handling or any specific scenarios you'd like to discuss!

4o mini

You said:

Give some assignments with solutions and explanation on above

ChatGPT said:

ChatGPT

Post a Comment

0Comments

Post a Comment (0)