codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
🐘

PHP

32 / 56 topics
30File Handling in PHP31File System Functions32Directories in PHP
Tutorials/PHP/Directories in PHP
🐘PHP

Directories in PHP

Updated 2026-05-15
10 min read

Directories in PHP

Introduction

In PHP, managing files and directories is a common task. Whether you're building a web application that requires file uploads or maintaining a logging system, understanding how to work with directories is essential. This tutorial will guide you through the basics of creating, reading, and manipulating directories in PHP.

Concept

Directories are essentially folders that contain other files or subdirectories. In PHP, you can perform various operations on directories such as creating them, deleting them, listing their contents, and checking if they exist. PHP provides a set of functions to handle these tasks efficiently.

Key Functions for Directory Operations

  • mkdir(): Creates a new directory.
  • rmdir(): Removes an empty directory.
  • scandir(): Reads the contents of a directory.
  • is_dir(): Checks if the specified path is a directory.
  • opendir(), readdir(), and closedir(): These functions are used to open, read, and close directories respectively.

Examples

Creating a Directory

To create a new directory in PHP, you can use the mkdir() function. This function takes two parameters: the path of the directory to be created and the mode (permissions).

php
1<?php
2$dir = 'new_directory';
3if (!file_exists($dir)) {
4 if (mkdir($dir, 0777, true)) {
5 echo "Directory '$dir' created successfully.";
6 } else {
7 echo "Failed to create directory '$dir'.";
8 }
9} else {
10 echo "Directory '$dir' already exists.";
11}
12?>

Removing a Directory

To remove an empty directory in PHP, you can use the rmdir() function. This function takes one parameter: the path of the directory to be removed.

php
1<?php
2$dir = 'new_directory';
3if (is_dir($dir)) {
4 if (rmdir($dir)) {
5 echo "Directory '$dir' removed successfully.";
6 } else {
7 echo "Failed to remove directory '$dir'.";
8 }
9} else {
10 echo "Directory '$dir' does not exist.";
11}
12?>

Listing Directory Contents

To read the contents of a directory, you can use the scandir() function. This function takes one parameter: the path of the directory to be read and returns an array containing the names of the entries in that directory.

php
1<?php
2$dir = 'new_directory';
3if (is_dir($dir)) {
4 $files = scandir($dir);
5 echo "Contents of '$dir':";
6 foreach ($files as $file) {
7 echo "<br>$file";
8 }
9} else {
10 echo "Directory '$dir' does not exist.";
11}
12?>

Checking if a Path is a Directory

To check if a given path is a directory, you can use the is_dir() function. This function takes one parameter: the path to be checked and returns true if the path is a directory, otherwise false.

php
1<?php
2$path = 'new_directory';
3if (is_dir($path)) {
4 echo "'$path' is a directory.";
5} else {
6 echo "'$path' is not a directory.";
7}
8?>

Using opendir(), readdir(), and closedir()

These functions provide more control over directory operations. opendir() opens a directory, readdir() reads the next entry in the directory, and closedir() closes the directory.

php
1<?php
2$dir = 'new_directory';
3if (is_dir($dir)) {
4 if ($dh = opendir($dir)) {
5 echo "Contents of '$dir':";
6 while (($file = readdir($dh)) !== false) {
7 echo "<br>$file";
8 }
9 closedir($dh);
10 } else {
11 echo "Failed to open directory '$dir'.";
12 }
13} else {
14 echo "Directory '$dir' does not exist.";
15}
16?>

What's Next?

Now that you have a good understanding of working with directories in PHP, the next step is to explore how to handle forms. Forms are essential for user input and interaction in web applications. You can learn more about processing form data, validating inputs, and handling file uploads in the upcoming tutorial on "Forms in PHP".

Feel free to experiment with these functions and build your own directory management tools in PHP!


PreviousFile System FunctionsNext Forms in PHP

Recommended Gear

File System FunctionsForms in PHP