使用带有自定义名称的PHP创建新文件夹

时间:2014-07-18 09:14:45

标签: php forms directory renaming

我已经使用此代码创建新文件夹如何创建具有名称表单客户端输入的新文件夹,并且我在第6行中收到错误

Warning: mkdir() [function.mkdir]: File exists in /home/a3629462/public_html/123.php on line 6

这是第6行的错误

$file_path = "uploads/";
$new_name = ""; // this is the new folder you'll create
$file_path .= $new_name . '/';
mkdir($file_path);
chmod($file_path, 0777);

$file_path = $file_path . basename( $_FILES['uploaded_file']['name']);
if(move_uploaded_file($_FILES['uploaded_file']['tmp_name'], $file_path)) {
    echo "success";
} else{
    echo "fail";
}

这是我使用的HTML表单我需要创建文件夹名称,并在标题框中输入输入文本

<form action="" method="post" enctype="multipart/form-data" name="addroom">
 Select Image: <br />
 <input type="file" name="uploaded_file" class="ed"><br />
Caption<br />
<input name="caption" type="text" class="ed" id="brnu" />
<br />
<input type="submit" name="Submit" value="Upload" id="button1" />
</form>

4 个答案:

答案 0 :(得分:0)

请尝试以下代码:

$path='your folder name here';

if (!file_exists($path)) 
{
    mkdir('path/to/directory', 0777, true);
}

答案 1 :(得分:0)

您还可以使用is_dir()检查目录是否存在。

答案 2 :(得分:0)

Try this code:
if (!is_dir($file_path)) {
     mkdir($file_path, 0777, true);
}

答案 3 :(得分:-2)

$input = $_POST['name_of_input']
// Do some stuff with it here
$file_path .= $input . "/";

if (!file_exists($file_path)) {
    mkdir($file_path);
}
相关问题