Why does PHP think this folder doesn't exist?

戏子无情 提交于 2019-12-02 09:31:21

First of all, you should adhere to the absolute patches when working with FileSystem, and also there are two minor flaws:

  • is_dir() - Checks whether file exists and its a directory. Therefore file_exists() is kinda redundant.

  • If you work with the same string anywhere else, it would be better to save its value in a variable.

And finally, your code should look like this,

$target = dirname(__FILE__) . '/hellodir';

if (is_dir($target)) {

    echo "folder already exists";

} else {

    echo "no folder, creating";
    // The 3-rd bool param includes recursion
    mkdir($target, 0777, true);
}

This will work as expected.

Try this code for creating folder if folder not exists there:

<?php 

if (file_exists('path/to/directory')) {
    echo "Folder Already Exists";
}
else{
    mkdir('path/to/directory', 0777, true);
    echo "folder Created";
}
?>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!