There is no problem not worth solving, and no technology not worth learning!

Error in PHP 7.4: mkdir(): file exists solution (error control operator: @ )

在 PHP 7.4 中报错:mkdir(): File exists

1. Report an error in PHP 7.4: mkdir(): file exists. as shown in Figure 1

在 PHP 7.4 中报错:mkdir(): File exists
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

第一次运行时,没有报错,目录:E:/wwwroot/currentFolder 被成功创建
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

第二次运行时,报错:Warning: mkdir(): File exists in E:\wwwroot\phpinfo.php on line 6
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

删除掉目录:E:/wwwroot/currentFolder,再次运行。没有报错,目录:E:/wwwroot/currentFolder 被成功创建
Figure 4

9. The final code is implemented as follows


if (!file_exists($prefix.DIRECTORY_SEPARATOR.$currentFolder)) {
	@mkdir($currentFolder, 0755);
	chmod($currentFolder, 0755);
}


Need long-term technical maintenance or remote troubleshooting?

I am a PHP / Go backend engineer with 15+ years of experience, focused on existing system maintenance, bug fixing, performance optimization, server troubleshooting, WordPress maintenance, and small feature iterations.

If your project is facing any of the following issues, we can start with a small troubleshooting task first:

  • ✅ PHP / Laravel / Yii2 legacy systems without active maintenance
  • ✅ Go / Gin backend APIs that need troubleshooting or optimization
  • ✅ Slow, broken, or unstable WordPress websites
  • ✅ Nginx / MySQL / Redis / Linux server issues
  • ✅ CDN / Cloudflare / DNS / HTTPS configuration problems
  • ✅ Long-term remote technical support or part-time maintenance

More details: About Me & Collaboration

WeChat: 13980074657
Email: shuijingwanwq@gmail.com
Telegram: @shuijingwan
GitHub: https://github.com/shuijingwan

评论

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.