Home  >  Article  >  Backend Development  >  How to use PHP to determine whether a file exists or not and then create a new file

How to use PHP to determine whether a file exists or not and then create a new file

王林
王林Original
2021-09-26 15:46:192819browse

php implements a method to determine whether a file exists or not and then create a new one: [function mkdirs($dir, $mode = 0777){if (is_dir($dir) || @mkdir($dir, $mode) ) return TRUE;if...].

How to use PHP to determine whether a file exists or not and then create a new file

The operating environment of this article: windows10 system, php 7, thinkpad t480 computer.

We generally make a judgment before creating a file. If the file to be created exists, it will not be created. If the file to be created does not exist, it will be created. So how to implement it using php code? The following is the specific implementation code, let’s take a look.

The code is as follows:

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);
}
 
 
mkdirs("aa01");

The default mode is 0777, which means the maximum possible access rights. For more information about mode please read the chmod() page.

Recommended learning: php training

The above is the detailed content of How to use PHP to determine whether a file exists or not and then create a new file. 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