PHP: Create folder using fopen

WBOY
Release: 2023-08-26 20:32:02
forward
1564 people have browsed it

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);
}
Copy after login

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!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!