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
https://forums.phpfreaks.com/topic/189206-creating-directories-with-php/
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. 

 

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.