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 tried to open the file using 'w' and 'w+' but it is not able to create new folder. Is there any way to achieve it without using mkdir(). Because I need to extract the directory names alone from the path to mkdir() everytime. Any help is appreciated. Thanks...
The fopen can't be used to create directories. This is because fopen function doesn't create or open folders, it only works with files. The above code creates a path to the file named 'filename'. The directory of the 'filename' is obtained using the 'dirname' function.
You can create a directory with PHP using the mkdir() function. mkdir("/path/to/my/dir", 0700); You can use fopen() to create a file inside that directory with the use of the mode w .
The fopen() function opens a file or URL.
The fopen function creates the file if it does not exist.
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); }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With