PHP 需要修复如何在文件夹已存在的情况下绕过 mkdir
我有一个 PHP 文件,它在上传文件时为每个用户创建一个唯一的目录。我希望脚本检查并查看该目录是否已存在,如果存在则跳过 mkdir 操作。这是我的代码示例:
<?php
$thisdir = getcwd();
$new_dir = "123";
$full_dir = $thisdir . "/upload/" . $new_dir;
if(mkdir($full_dir, 0777))
{
echo "Directory has been created successfully... <br>";
}
else
{
echo "Failed to create directory...";
}
?>
要继续此示例,请假设文件夹“123”已存在。对于这种情况我该如何修改呢?我想这一定是某种 if...else 语句。感谢您经历这个问题。
I have a PHP file which creates a unique directory for each user when a file is uploaded. I would like the script to check and see if the directory already exists and if so then skip the mkdir action. Here is my code sample:
<?php
$thisdir = getcwd();
$new_dir = "123";
$full_dir = $thisdir . "/upload/" . $new_dir;
if(mkdir($full_dir, 0777))
{
echo "Directory has been created successfully... <br>";
}
else
{
echo "Failed to create directory...";
}
?>
To continue this example, please assume that the folder "123" already exists. How do I modify it for this case? I am thinking it must be some sort of if...else statement. Thanks for going through this issue.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用
is_dir()
来查明该文件夹是否已存在。Use
is_dir()
to find out whether the folder already exists.