Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/268.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/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 如果目录已经存在,如何退出脚本?_Php_Loops_For Loop_Exit Code_File Exists - Fatal编程技术网

Php 如果目录已经存在,如何退出脚本?

Php 如果目录已经存在,如何退出脚本?,php,loops,for-loop,exit-code,file-exists,Php,Loops,For Loop,Exit Code,File Exists,下面有一个PHP代码,可以在当前目录中创建多个目录。 如果目录已经存在,我需要终止脚本。 但是,我收到了多条“文件存在”警告消息 代码 <?php //Creates multiple directories inside the current directory. $curdir = getcwd(); $slash = "/0"; for ($dir = 1; $dir <= 12; $dir++){ $concat = $slash . $dir;

下面有一个PHP代码,可以在当前目录中创建多个目录。
如果目录已经存在,我需要终止脚本。
但是,我收到了多条“文件存在”警告消息

代码

<?php

//Creates multiple directories inside the current directory.

$curdir = getcwd();
$slash = "/0";

for ($dir = 1; $dir <= 12; $dir++){

      $concat = $slash . $dir;

      if (!file_exists($concat)) {

        mkdir($curdir . "$concat" , 0777);

      } else {

        exit("Files already exist.");

      }

} 

?>

您必须编辑if语句。您正在检查的目录与创建的目录不同:

<?php

$curdir = getcwd();
$slash = "/0";

for ($dir = 1; $dir <= 12; $dir++){

      $concat = $slash . $dir;

      if (!file_exists($curdir . $concat)) {

        mkdir($curdir . $concat , 0777);

      } else {

        exit("Files already exist.");

      }

} 

我是新用户。我试着投票支持你,但我不同意。我会在5分钟内接受你的回答。这将是一个梦想。我感谢你的帮助解决方案
<?php

$curdir = getcwd();
$slash = "/0";

for ($dir = 1; $dir <= 12; $dir++){

      $concat = $slash . $dir;

      if (!file_exists($curdir . $concat)) {

        mkdir($curdir . $concat , 0777);

      } else {

        exit("Files already exist.");

      }

}