Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

php: create directory on form submit?

Tags:

php

mkdir

I am wondering what I am doing wrong. I'm inside of PATH and I want to create a folder inside of PATH. I want to check if the folder already exists and, if not, create one. Getting the name of the folder from an input field 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
    }
}
like image 474
matt Avatar asked Dec 03 '22 11:12

matt


1 Answers

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 ;)

like image 129
thecodeassassin Avatar answered Dec 20 '22 03:12

thecodeassassin