Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php _dir()是否不可靠,或者是否存在可以缓解的竞争条件?_Php_Directory_Php 5.4 - Fatal编程技术网

Php _dir()是否不可靠,或者是否存在可以缓解的竞争条件?

Php _dir()是否不可靠,或者是否存在可以缓解的竞争条件?,php,directory,php-5.4,Php,Directory,Php 5.4,在工作中,我继承了一个具有文件上传过程的web应用程序。此过程的一部分偶尔(大约每两周一次)会触发以下错误: PHP Warning: mkdir(): File exists in {{{file_path_name_redacted}}} on line 7 查看第6-8行,我们可以看到: if(!is_dir($storeFolder)){ mkdir($storeFolder, 0644, TRUE); } 鉴于此文件可能会被多个PHP进程命中,我相信竞争条件可能会在这里发

在工作中,我继承了一个具有文件上传过程的web应用程序。此过程的一部分偶尔(大约每两周一次)会触发以下错误:

PHP Warning:  mkdir(): File exists in {{{file_path_name_redacted}}} on line 7
查看第6-8行,我们可以看到:

if(!is_dir($storeFolder)){
    mkdir($storeFolder, 0644, TRUE);
}
鉴于此文件可能会被多个PHP进程命中,我相信竞争条件可能会在这里发挥作用。我在过去管理过的其他网站上也遇到过同样的问题,类似的情况在每个月都会发生一次

我认为现在发生的是,用户双击upload按钮,导致两个PHP进程几乎完全同时执行,如下所示:

Process 1 executes line 6 - dir does not exist
Process 2 executes line 6 - dir does not exist
Process 1 executes line 7 - directory is created
Process 2 executes line 7 - directory cannot be created as it already exists

正如我在上文中所解释的,这是否属于竞争条件(即,是否有其他人注意到了这一点),和/或是否有其他方法可以减少错误,或者关闭错误报告以获取警告?

Php检查确认a,并建议编写代码的最安全方法是:

if (!is_dir($dir) && !mkdir($dir) && !is_dir($dir)) {
        throw new \RuntimeException(sprintf('Directory "%s" could not be created', $dir));
    }


这感觉很奇怪,但确实有效。祝您好运。

可能值得检查
文件\u exists()
,否则如果
$storeFolder
作为文件(而不是目录)存在,则
is\u dir()
将有效返回false,但
mkdir()
将失败我将尝试,但父文件夹中的工件仅通过此过程创建。i、 e.folderI中只有目录,因为我已经离开了以前的工作场所,所以我无法再进行测试,但您提供的链接准确地解释了这种情况。如果这样做有效,我不会感到惊讶,因为
is_dir
mk_dir
可能会调用不同队列的不同进程。谢谢你的回答!