2

我对此有点麻烦。以为会更容易,结果却很沮丧。我要做的只是有一个文本字段,我可以在其中键入新目录的名称,检查该目录是否存在,如果不存在则创建它。我发现大约 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";
            }
        }
}
4

4 回答 4

14

改变这个:

if(!is_dir($new_path)) {
    echo "The Directory {$new_path} exists";
    }

对此:

if(is_dir($new_path)) {
    echo "The Directory {$new_path} exists";
    }

试着告诉我结果:)

于 2013-04-17T02:27:54.420 回答
1

而不是is_dir在 if 块中使用,您可以使用file_exists. 因为 file_exists 是检查文件是否存在的函数。同样你也可以参考http://php.net/manual/en/function.file-exists.php

于 2013-04-17T02:26:38.057 回答
1

尝试

if( is_dir( $new_path ) ) {
    echo "The Directory {$new_path} exists";
}
于 2013-04-17T02:58:00.650 回答
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"]);
于 2016-11-25T15:33:12.663 回答