tags:

views:

143

answers:

3

hey guys, wondering what i'm doing wrong. first time i'm using this function. i'm inside of PATH and i want to create a folder inside of PATH. i wann check if the folder already exists, if not create one. getting the name of the folder from an inputfield with name of "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
    }
}

thank guys

+2  A: 

You can use is_dir().

vtorhonen
+3  A: 

Hi there!

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

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 a good hint ;)

Stephen
If there is a file named $dir when is_dir is run, it'll return false, because it's not a dir, but the subsequent mkdir will fail because the file already exists. It's better to use file_exists.
jmz
+1  A: 

You have to use

!is_dir($dir)

instead of

!file_exists($dir)

it's not a file, it's a directory!

Good luck!

codeworxx