如果目录已经存在,如何退出脚本?
How to exit the script if the directories already exist?
下面有一个 PHP 代码可以在当前目录中创建多个目录。
如果目录已经存在,我需要终止脚本。
但相反,我收到多条 "file exists" 警告消息。
代码
<?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 14
Warning: mkdir(): File exists in /opt/lampp/htdocs/SQL/mkdir.php on line 14
Warning: mkdir(): File exists in /opt/lampp/htdocs/SQL/mkdir.php on line 14
Warning: mkdir(): File exists in /opt/lampp/htdocs/SQL/mkdir.php on line 14
Warning: mkdir(): File exists in /opt/lampp/htdocs/SQL/mkdir.php on line 14
Warning: mkdir(): File exists in /opt/lampp/htdocs/SQL/mkdir.php on line 14
Warning: mkdir(): File exists in /opt/lampp/htdocs/SQL/mkdir.php on line 14
Warning: mkdir(): File exists in /opt/lampp/htdocs/SQL/mkdir.php on line 14
Warning: mkdir(): File exists in /opt/lampp/htdocs/SQL/mkdir.php on line 14
Warning: mkdir(): File exists in /opt/lampp/htdocs/SQL/mkdir.php on line 14
Warning: mkdir(): File exists in /opt/lampp/htdocs/SQL/mkdir.php on line 14
Warning: mkdir(): File exists in /opt/lampp/htdocs/SQL/mkdir.php on line 14
您必须编辑您的 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.");
}
}
下面有一个 PHP 代码可以在当前目录中创建多个目录。
如果目录已经存在,我需要终止脚本。
但相反,我收到多条 "file exists" 警告消息。
代码
<?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 14
Warning: mkdir(): File exists in /opt/lampp/htdocs/SQL/mkdir.php on line 14
Warning: mkdir(): File exists in /opt/lampp/htdocs/SQL/mkdir.php on line 14
Warning: mkdir(): File exists in /opt/lampp/htdocs/SQL/mkdir.php on line 14
Warning: mkdir(): File exists in /opt/lampp/htdocs/SQL/mkdir.php on line 14
Warning: mkdir(): File exists in /opt/lampp/htdocs/SQL/mkdir.php on line 14
Warning: mkdir(): File exists in /opt/lampp/htdocs/SQL/mkdir.php on line 14
Warning: mkdir(): File exists in /opt/lampp/htdocs/SQL/mkdir.php on line 14
Warning: mkdir(): File exists in /opt/lampp/htdocs/SQL/mkdir.php on line 14
Warning: mkdir(): File exists in /opt/lampp/htdocs/SQL/mkdir.php on line 14
Warning: mkdir(): File exists in /opt/lampp/htdocs/SQL/mkdir.php on line 14
Warning: mkdir(): File exists in /opt/lampp/htdocs/SQL/mkdir.php on line 14
您必须编辑您的 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.");
}
}