PHP: fopen to create folders

后端 未结 3 926
独厮守ぢ
独厮守ぢ 2020-12-08 19:36

I need to know if there is any way to create new folder if the path doesn\'t exist. When I try to fopen() a path, it says NO such File or Directory exists I tri

相关标签:
3条回答
  • 2020-12-08 19:49

    fopen doesn't create or open folders, only files. You should check with is_dir first if it exists, if not create it. mkdir has a recursive create option.

    if (!is_dir($myDir)) {
        mkdir($myDir, 0777, true); // true for recursive create
    }
    

    If you are looking for a way to open a dir and read it's content you should look at SPL's DirectoryIterator

    0 讨论(0)
  • 2020-12-08 19:51

    you can't use fopen to create folders.
    To create a folder you have to use mkdir

    for the operations you have to repeat every time, there is a language feature called "user-defined functions". Least known feature of PHP, as one can say judging by stackoverflow answers.

    0 讨论(0)
  • 2020-12-08 20:06

    fopen cannot create directories.

    You'll need to use something like:

    $filename = '/path/to/some/file.txt';
    $dirname = dirname($filename);
    if (!is_dir($dirname))
    {
        mkdir($dirname, 0755, true);
    }
    
    0 讨论(0)
提交回复
热议问题