-1

i have used this code to create new folder how can i create new folder with a name form client input and i get error in line six

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

that's the error am getting in line 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";
}

this the HTML form i used i need to create folder name with input text entered in caption box

<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 Answers4

0

Try the following code :

$path='your folder name here';

if (!file_exists($path)) 
{
    mkdir('path/to/directory', 0777, true);
}
Sulthan Allaudeen
  • 11,330
  • 12
  • 48
  • 63
0

You can also use is_dir() to check if directory exists.

fdehanne
  • 1,658
  • 1
  • 16
  • 32
0
Try this code:
if (!is_dir($file_path)) {
     mkdir($file_path, 0777, true);
}
Indrajeet Singh
  • 2,958
  • 25
  • 25
-2
$input = $_POST['name_of_input']
// Do some stuff with it here
$file_path .= $input . "/";

if (!file_exists($file_path)) {
    mkdir($file_path);
}
Jonathon
  • 15,873
  • 11
  • 73
  • 92
  • Well if you're posting to this PHP script, get the value of the input text using `$_POST['name_of-input']` and use this – Jonathon Jul 18 '14 at 09:23