Home  >  Article  >  Backend Development  >  PHP: Create folder using fopen

PHP: Create folder using fopen

WBOY
WBOYforward
2023-08-26 20:29:101534browse

PHP: 使用fopen创建文件夹

fopen cannot be used to create directories. This is because the fopen function does not create or open folders, it only works with files.

Before using the fopen function, you should first check whether is_dir exists. If it does not exist, use the mkdir function to create it -

$filename = '/path/to /file.txt';
$dirname = dirname($filename);
if (!is_dir($dirname)) {
   mkdir($dirname, 0755, true);
}

The above code creates a file named "filename" path. Use the "dirname" function to get the directory of "filename". Next, use the "is_dir" function to check if the directory exists. If the directory already exists, no action is taken. On the other hand, if the directory does not exist, then the "mkdir" function is used to create the directory by passing specific access permissions.

The above is the detailed content of PHP: Create folder using fopen. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete