Jump to content

Creating directories with PHP


mattheww

Recommended Posts

Its fine creating intial directories, but if i want to create a directory within a directory i am running into problems, here teh code i have so far...

  if ($_POST['createdir']){
    $newdir = $_POST['name'];
    $dir = $_GET['dir'];
    $root = getcwd();
    $create = mkdir("$root/dir/$dir/$newdir", 0700);
  }

it basically takes the directory name from the URL... where it says /$dir if i replace that for it's text equivalent, and don't use a variable it works fine, but i need it to work... However i don't know what i'm doing wrong!

 

Link to comment
Share on other sites

When you don't know what's happening, you have to inject something to help you debug it.  If you have the option, XDebug can also be very helpful, in terms of the additional information it can provide to you when there are errors.

 

I'd suggest you first try and debug this by setting the path you're trying to create to a variable and echoing it out.

 

if ($_POST['createdir']){
    $newdir = $_POST['name'];
    $dir = $_GET['dir'];
    $root = getcwd();
    $path = "$root/dir/$dir/$newdir";
    echo "Creating ... $path";
    $create = mkdir("$root/dir/$dir/$newdir", 0700);
  }

 

Some other notes: 

- mkdir returns true/false.  Your code is better if you check the result and handle errors.

- You should check to see if the directory already exists.  If it does mkdir fails. 

 

Link to comment
Share on other sites

It could also be a permission problem.  If apache can't write to the folder to create the directory, it'll fail.  As gizmola suggested, the best approach is to write logic for debugging to see exactly what is causing the error.  When all else fails, display php errors or view the php error log.

 

error_reporting(E_ALL);
ini_set('display_errors','on');

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.