php 检查目录是否存在,创建目录
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16050339/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Check if directory exists, make directory
提问by tunedin
I'm having a little trouble with this. Thought it'd be easier, but turning out to frustrate. All I'm trying to do is have a text field where I can type the name of a new directory, check if that directory exists, and if not create it. I have found about 50 other people with almost the exact same code, so thought I had it correct but I keep getting Directory exists as per the "if" statement.
我在这方面有点麻烦。以为会更容易,但结果令人沮丧。我想要做的就是有一个文本字段,我可以在其中键入新目录的名称,检查该目录是否存在,如果不存在,则创建它。我发现大约 50 个其他人的代码几乎完全相同,所以我认为我的代码是正确的,但我一直根据“if”语句获取目录存在。
Eventually I want to tie this into my file upload script.
最终我想把它绑定到我的文件上传脚本中。
Here is the insert.php
这是insert.php
<form action="" method="post" enctype="multipart/form-data" name="form1" id="form1">
<p>
<label for="directory">Directory:</label>
<input value="<?php if ($_POST && $errors) {
echo htmlentities($_POST['directory'], ENT_COMPAT, 'UTF-8');
}?>" type="text" name="directory" id="directory" />
</p>
<p>
<input type="submit" name="insert" id="insert" value="insert" />
</p>
</form>
And here is the post.php
这是 post.php
try {
if (isset($_POST['insert'])) {
$directory = $_POST['directory'];
$photo_destination = 'image_upload/';
$path = $photo_destination;
$new_path = $path . $directory;
$mode = 0755;
if(!is_dir($new_path)) {
echo "The Directory {$new_path} exists";
} else {
mkdir($new_path , 0777);
echo "The Directory {$new_path} was created";
}
}
}
回答by Last Breath
Change this :
改变这一点:
if(!is_dir($new_path)) {
echo "The Directory {$new_path} exists";
}
to this :
对此:
if(is_dir($new_path)) {
echo "The Directory {$new_path} exists";
}
Try and tell me the result :)
试着告诉我结果:)
回答by Tamil Selvan C
Try
尝试
if( is_dir( $new_path ) ) {
echo "The Directory {$new_path} exists";
}
回答by Mahabubur Rahman Masum
Let someone wants to create a sub-folder,
with a year name under /uploads
folder
then he/she want to create
another sub-folder under an /uploads/<year_name_folder>/
,
named project_number
.
让某人想要创建子文件夹,
用下一年名称/uploads
的文件夹
,那么他/她想要创建下另一个子文件夹/uploads/<year_name_folder>/
,
命名project_number
。
$yearfolder = date('y');
if (!file_exists('uploads/'.$yearfolder)) {
mkdir("uploads/".$yearfolder);
}
*// Folder named by year has been created.*
$project_number = Any Unique field ,Come from database or anywhere !
$target_directory = mkdir("uploads/".$yearfolder."/".$project_number);
*// project number wise folder also created.
// If project number is not unique do check like year folder. I think every project number is unique.*
$target_dir = "uploads/$yearfolder/$project_number/";
*//my target dir has created where my document or pic whatever will be uploaded.*
Now Upload ! Woo
$target_file = $target_dir.($_FILES["file"]["name"]);
回答by Ankur Kumar Singh
Instead of using is_dir
in the if block you can use file_exists
. Because file_exists is the function to check whether file exists or not. For the same you can also refer to http://php.net/manual/en/function.file-exists.php
而不是is_dir
在 if 块中使用,您可以使用file_exists
. 因为 file_exists 是检查文件是否存在的函数。同样的你也可以参考http://php.net/manual/en/function.file-exists.php
回答by Sujeet Kumar
<?php
$dirname = "small";
$filename = "upload/".$dirname."/";
if (!is_dir($filename )) {
mkdir("upload/" . $dirname, 0777, true);
echo "The directory $dirname was successfully created.";
exit;
} else {
echo "The directory $dirname exists.";
}
?>