mattheww Posted January 20, 2010 Share Posted January 20, 2010 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! Quote Link to comment https://forums.phpfreaks.com/topic/189206-creating-directories-with-php/ Share on other sites More sharing options...
jamesxg1 Posted January 20, 2010 Share Posted January 20, 2010 Try if ($_POST['createdir']){ $newdir = $_POST['name']; $dir = $_GET['dir']; $root = getcwd(); $create = mkdir($root . '/dir/' . $dir . '/' . $newdir . '/', 0700); } Quote Link to comment https://forums.phpfreaks.com/topic/189206-creating-directories-with-php/#findComment-998876 Share on other sites More sharing options...
mattheww Posted January 20, 2010 Author Share Posted January 20, 2010 unfortunately it didn't work , thanks for your suggestion anyway! Quote Link to comment https://forums.phpfreaks.com/topic/189206-creating-directories-with-php/#findComment-998881 Share on other sites More sharing options...
gizmola Posted January 20, 2010 Share Posted January 20, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/189206-creating-directories-with-php/#findComment-998884 Share on other sites More sharing options...
p2grace Posted January 20, 2010 Share Posted January 20, 2010 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'); Quote Link to comment https://forums.phpfreaks.com/topic/189206-creating-directories-with-php/#findComment-999040 Share on other sites More sharing options...
laffin Posted January 21, 2010 Share Posted January 21, 2010 could also be that mkdir doesnt handle subdir creation. in other words, it cant make a dir if the parent doesnt exixst. but it could be a lot of things. as Gizmola said, have to trace down the problem. than come up with a solution. Quote Link to comment https://forums.phpfreaks.com/topic/189206-creating-directories-with-php/#findComment-999056 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.