为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

PHP
慕姐8265434 2022-09-25 20:23:28
下面有一个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.");      }} ?>输出Warning: mkdir(): File exists in /opt/lampp/htdocs/SQL/mkdir.php on line 14Warning: mkdir(): File exists in /opt/lampp/htdocs/SQL/mkdir.php on line 14Warning: mkdir(): File exists in /opt/lampp/htdocs/SQL/mkdir.php on line 14Warning: mkdir(): File exists in /opt/lampp/htdocs/SQL/mkdir.php on line 14Warning: mkdir(): File exists in /opt/lampp/htdocs/SQL/mkdir.php on line 14Warning: mkdir(): File exists in /opt/lampp/htdocs/SQL/mkdir.php on line 14Warning: mkdir(): File exists in /opt/lampp/htdocs/SQL/mkdir.php on line 14Warning: mkdir(): File exists in /opt/lampp/htdocs/SQL/mkdir.php on line 14Warning: mkdir(): File exists in /opt/lampp/htdocs/SQL/mkdir.php on line 14Warning: mkdir(): File exists in /opt/lampp/htdocs/SQL/mkdir.php on line 14Warning: mkdir(): File exists in /opt/lampp/htdocs/SQL/mkdir.php on line 14Warning: mkdir(): File exists in /opt/lampp/htdocs/SQL/mkdir.php on line 14
查看完整描述

1 回答

?
繁花不似锦

TA贡献1851条经验 获得超4个赞

您必须编辑 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.");


      }



查看完整回答
反对 回复 2022-09-25
  • 1 回答
  • 0 关注
  • 58 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信