Home > Backend Development > PHP Problem > 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

王林
Release: 2023-03-13 06:18:02
Original
2842 people have browsed it

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

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!

Related labels:
source:php.cn
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