Error in PHP 7.4: mkdir(): file exists solution (error control operator: @ )
1. Report an error in PHP 7.4: mkdir(): file exists. as shown in Figure 1
if (!file_exists($prefix.DIRECTORY_SEPARATOR.$currentFolder)) {
mkdir($currentFolder, 0755);
chmod($currentFolder, 0755);
}
2. However, in the code implementation, it is first judged that the directory does not exist before executing mkdir(). But at the time of execution, the directory already exists. It should be a problem caused by request concurrency.
3. Reproduce this error in the local environment
$currentFolder = 'E:/wwwroot/currentFolder';
mkdir($currentFolder, 0755);
chmod($currentFolder, 0755);
4. During the first run, no errors were reported, and the directory: e:/wwwroot/currentfolder was successfully created. as shown in Figure 2
5. During the second run, an error is reported: warning: mkdir(): file exists in e:\wwwroot\phpinfo.php on line 6. as shown in Figure 3
6. Adjust the code implementation, use the error control operator: @ Solve. Test code runs in the local environment.
$currentFolder = 'E:/wwwroot/currentFolder';
@mkdir($currentFolder, 0755);
chmod($currentFolder, 0755);
7. During the third run, there is no error.
8. Delete the directory: e:/wwwroot/currentfolder, run again. No error is reported, directory: e:/wwwroot/currentFolder was successfully created. as shown in Figure 4
9. The final code is implemented as follows
if (!file_exists($prefix.DIRECTORY_SEPARATOR.$currentFolder)) {
@mkdir($currentFolder, 0755);
chmod($currentFolder, 0755);
}



