Home  >  Article  >  Backend Development  >  What to do if the php fopen file does not exist

What to do if the php fopen file does not exist

藏色散人
藏色散人Original
2021-06-16 09:49:432696browse

php fopen file does not exist solution: first create a PHP sample file; then use the "if (!function_exists('mkdirs')) {...}" method to determine whether the folder exists, if not, then Just create a new one.

What to do if the php fopen file does not exist

The operating environment of this article: windows7 system, PHP7.1 version, DELL G3 computer

What should I do if the php fopen file does not exist?

Determine whether the folder exists in php. If it does not exist, create a new one.

Introduction: During the project development process, sometimes it is necessary to read or generate files in the specified folder. At this time, you need to first determine whether the folder exists, and if it does not exist, create a new one.

Method:

//判断文件夹是否存在,没有则新建。
if (!function_exists('mkdirs')) {
    function mkdirs($dir, $mode = 0777)
    {
        if (is_dir($dir) || @mkdir($dir, $mode)) {
            return true;
        }
        if (!mkdirs(dirname($dir), $mode)) {
            return false;
        }
        return @mkdir($dir, $mode);
    }
}

Actual use:

    //判断文件夹是否存在
    public function test()
    {
        $localhost_path = ROOT_PATH . 'public/static/unit_record/';
        $result = mkdirs($localhost_path);//这里会返回true
    }

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of What to do if the php fopen file does not exist. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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