Check if a folder already created using PHP

You can use this code to check whether a folder or directory already exists or not. We are using a PHP script to check that.

    $newdirname = $_POST["new_dir_name"];
    $dir_path = "path/to/" . $newdirname . "/";

    if (!file_exists($dir_path)) {
        mkdir($dir_path, 0777);
        echo "The directory creted successfully.";
    } else {
        echo "The directory already exists.";
Was this helpful?