Contact!

Recursively make directories in PHP using mkdir

Recursively make directories in PHP using mkdir

Quite often you will need to create or edit files in a PHP application. The problem is that you will probably need to create the directory structure at some stage. Therefore, it is useful to know how to recursively make directories easily.

To do this we are first going to check whether the current directory structure exists. If it doesn’t, we are going to recursively create all the folders using mkdir(). You might have guessed, but the purpose of this function is creating directory paths. You can directly create a directory or recusively create the directory structure by passing a boolean flag.

We’ll start with a function, and pass it a path variable. This will serve as our base:

function makeDirPath($path) {

}

Check whether the current directory exists

We can check whether a directory exists using either file_exists() or is_dir(). The thing is, is_dir() is actually telling us whether the given path/file is a directory, and therefore it returns false if the directory doesn’t exist. file_exists() checks whether the file or directory exists, and as we are checking whether a directory exists I strongly suggest we use it.

function makeDirPath($path) {
    return file_exists($path);
}

Note: We are checking whether the file exists first to avoid errors later.

Recursively make directories if they don’t exist

Our function is currently pretty useless. It returns true or false depending on whether a directory exists or not. The next step is to use mkdir() when file_exists() returns false.

To do this we are going to use an or (||) operator:

function makeDirPath($path) {
    return file_exists($path) || mkdir($path, 0777, true);
}

We are passing mkdir the path, the file permissions (0777 is default, and it is ignored in windows), and setting the recursive flag to ‘true’. If the file doesn’t exist, the entire directory structure will be recursively created.

Drop the function into your project and call it whenever you need a new directory.

  1. Anon says:

    How is this article written on 05/03/2021 when today in only 03/16/2021 ?????

    1. Eric says:

      because : DD/MM/YYYY

      1. Seba says:

        Nope, he’s come from the future 😎 haha

        1. Seba says:

          He comes from* Sorry, my english is not pretty good 🙁

  2. Jane Doe says:

    How comes you guys notice such details in web pages …. ?????

Join the discussion!

You might like:

Create, register and use shortcodes in WordPress

Create, register and use shortcodes in WordPress

by Gav 18/03/2023

Learn how to create and register your own WordPress shortcodes to add dynamic content to your posts and pages.

How to use guard clauses in JavaScript

How to/why use guard clauses in JavaScript

by Gav 16/03/2023

Learn how to improve code readability and performance by using guard clauses in JavaScript. Discover their benefits and best practices.

Implements and Extends, Object Oriented TypeScript

Implements and Extends, Object Oriented TypeScript

by Gav 15/03/2023

Learn the difference between implements and extends in TypeScript. Use Implements to implement interfaces and types, and extends to inherit from classes.

Reading/Parsing and Writing YAML files in PHP, Symfony

Reading/Parsing and Writing YAML files, PHP Symfony

by Gav 14/03/2023

In this tutorial we will look at using YAML in PHP. Learn about Parsing and Writing YAML files using Symfony's YAML component.

Measuring code execution performance in JavaScript

Measuring code execution performance in JavaScript

by Gav 13/03/2023

Measuring code execution performance is an important way to identify bottlenecks. Use these methods in JavaScript to help optimise your code.

Measuring script/code execution time in PHP using microtime

Measuring script/code execution time in PHP, microtime

by Gav 06/03/2023

Find bottlenecks, optimise and clean your code, and speed up your apps by measuring the execution time of your PHP scripts using microtime.

Regenerate WordPress media image sizes, programmatically

Regenerate WordPress media image sizes, programmatically

by Gav 25/02/2021

Learn how to regenerate and update WordPress media and image sizes both programmatically (without plugin), and also with a handy plugin.

Magic Constants in PHP. What they are and how to use them.

Magic Constants in PHP. What they are and how to use them

by Gav 15/02/2021

Ever seen constants like __DIR__ and __FILE__ being used in PHP? These are 'Magic Constants', and this is how we can use them.

Detecting Keypress JavaScript

Detect single and multiple keypress events: JavaScript

by Gav 16/10/2019

Learn how to use event listeners to detect and handle single and multiple keypress events in JavaScript. Add modifier keys to your application!