我遇到这个问题。认为它会更容易,但结果却令人沮丧。我要做的就是有一个文本字段,我可以在其中键入新目录的名称,检查该目录是否存在,如果不存在则创建它。我发现大约有50个人的代码几乎完全相同,所以我认为我的方法是正确的,但我仍然按照“if”语句继续使用Directory。
最终我想把它绑定到我的文件上传脚本中。
这是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>
这是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";
}
}
}
答案 0 :(得分:11)
改变这个:
if(!is_dir($new_path)) {
echo "The Directory {$new_path} exists";
}
到此:
if(is_dir($new_path)) {
echo "The Directory {$new_path} exists";
}
试着告诉我结果:)
答案 1 :(得分:1)
您可以使用is_dir
而不是在if块中使用file_exists
。因为file_exists是检查文件是否存在的函数。同样,您也可以参考http://php.net/manual/en/function.file-exists.php
答案 2 :(得分:1)
尝试
if( is_dir( $new_path ) ) {
echo "The Directory {$new_path} exists";
}
答案 3 :(得分:1)
让某人想要创建一个子文件夹,
在 /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"]);
答案 4 :(得分:0)
<?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.";
}
?>