mkdir() 文件存在

问题描述 投票:0回答:3

我试图在发送电子邮件和密码时在图像文件夹中创建一个新目录,它检索用户的 ID 并根据用户 ID 在图像文件夹中创建一个文件夹,但是它不起作用,因为我得到这个错误:

Warning: mkdir(): File exists in C:\Users\authenticate.php on line 101

这是我尝试过的,这就是它发出警告的内容:

  // Otherwise, the result variable passes on the confirm-email and the confirm-password to the login function
               $result = $userVeri->login(strtolower($_POST["confirm-email"]), $_POST["confirm-password"]);
               // The row variable stores the result
               $row = $result;
               // Then make a directory in the images folder with the new user id and give the folder all priveleges
               mkdir('images/'.$row["id"],0777);
               exit;
               // Then display this message
               echo '<div class="alert alert-success">Congratulations! your account has been created. Please sign in.</div>';

有人明白我做错了什么吗?

顺便说一句,即使图像文件夹中不存在文件夹,它也会发出此警告

php mkdir
3个回答
2
投票

使用 PHP 的

is_dir($path_to_dir)
检查之前的目录是否存在。或者您可以使用此代码

if (!file_exists($path)) {
    mkdir($path, 0700);
}

0
投票
// Otherwise, the result variable passes on the confirm-email and the confirm-password to the login function
$result = $userVeri->login(strtolower($_POST["confirm-email"]), $_POST["confirm-password"]);
// The row variable stores the result
$row = $result;
// Then make a directory in the images folder with the new user id and give the folder all priveleges
mkdir(__DIR__.'/images/'.$row["id"],0777);
// Then display this message
echo '<div class="alert alert-success">Congratulations! your account has been created. Please sign in.</div>';

我没有添加对 mkdir 是否成功或登录方法返回的值的检查。 重新检查了手册以了解我的记忆想法,发现首选方法现在是用魔术常量

__DIR__
代替(其中包含脚本文件的路径)


0
投票

错误

Warning: mkdir(): File exists
Warning: rmdir(): File exists
表明该文件夹不为空。 PHP 只能删除空文件夹,mkdir 只能创建不存在的目录。

必须先删除文件夹文件。

// A directory somedir exist and contain files
$files = glob("somedir/*");
foreach($files as $file){
    unlink("$file");
}
rmdir("somedir/"); // Now works
mkdir("somedir");  // Now works
© www.soinside.com 2019 - 2024. All rights reserved.