Php: create a directory in form submit?

I am wondering what I am doing wrong. I am inside PATH and I want to create a folder inside PATH. I want to check if a folder exists, and if not, create it. Getting the folder name from the input field with the name "dirname".

if (isset($_POST['createDir'])) { //get value of inputfield $dir = $_POST['dirname']; //set the target path ?? $targetfilename = PATH . '/' . $dir; if (!file_exists($dir)) { mkdir($dir); //create the directory chmod($targetfilename, 0777); //make it writable } } 
+4
source share
4 answers

It might be a good idea to make sure that the directory you are processing is indeed a directory. This code works ... edit as you like.

 define("PATH", "/home/born05/htdocs/swish_s/Swish"); $test = "set"; $_POST["dirname"] = "test"; if (isset($test)) { //get value of inputfield $dir = $_POST['dirname']; //set the target path ?? $targetfilename = PATH . '/' . $dir; if (!is_file($dir) && !is_dir($dir)) { mkdir($dir); //create the directory chmod($targetfilename, 0777); //make it writable } else { echo "{$dir} exists and is a valid dir"; } 

Good luck

Edited: comment was good advice;)

+10
source

You have to use

 !is_dir($dir) 

instead

 !file_exists($dir) 

this is not a file, this is a directory!

Good luck

+4
source

You can use is_dir () .

+3
source

@codeworxx file_exists can also be used to check the directory.

http://www.php.net/manual/en/function.file-exists.php

0
source

All Articles