Append Posted March 15, 2011 Share Posted March 15, 2011 Hello, I was wondering if it was possible to do mkdir() on a windows server, I know it is so I'm going to continue asking my question. I've to make a form, with the action as 'something.php' for example. The 'something.php' file has this in it '$name = $_POST['filename'];''. I was wondering if it was possible to get what was imputted in the form that I did in HTML, to make a file in 'C:\thirdir\$name'. That probably doesn't make any sence, if you get it you're a legend. Quote Link to comment https://forums.phpfreaks.com/topic/230693-mkdir/ Share on other sites More sharing options...
Adam Posted March 15, 2011 Share Posted March 15, 2011 I'm confused. Do you want to create a file or a directory? Either should be possible, with the correct permissions. Quote Link to comment https://forums.phpfreaks.com/topic/230693-mkdir/#findComment-1187731 Share on other sites More sharing options...
Append Posted March 15, 2011 Author Share Posted March 15, 2011 Lol, I want to make a directory, within a directory. C:\thedir\$namestring Quote Link to comment https://forums.phpfreaks.com/topic/230693-mkdir/#findComment-1187733 Share on other sites More sharing options...
Adam Posted March 15, 2011 Share Posted March 15, 2011 Okay, just you spoke about creating both a file and directory. The mkdir() function should work fine for that, assuming the directory has the correct permissions. I would also validate the file name, so that the path cannot be altered (by "../" for example), though I'm not sure how applicable that is in this situation.. if (!empty($_POST['dirname'])) { $name = $_POST['dirname']; // Check the dir name doesn't contain "..", "/" or "\" - you'll // want to build on this for more comprehensive validation if (!preg_match('/[(\.\.)\/\\\]/', $name)) { // Note the double backslash $path = 'C:\path\to\\'; // Create the dir if (mkdir($path . $dir)) { echo 'Success'; } else { echo 'Failure'; } } else { echo 'Invalid directory name'; } } As I said before though, this will only work if the user PHP is running as, has permission to write to that directory. You'll probably get an error if it can't. Quote Link to comment https://forums.phpfreaks.com/topic/230693-mkdir/#findComment-1187736 Share on other sites More sharing options...
Append Posted March 15, 2011 Author Share Posted March 15, 2011 It makes the directory fine, thankyou. I was wondering how I can make the directory name what was imputted into the text box on the form? Hope this is possible, Append. Quote Link to comment https://forums.phpfreaks.com/topic/230693-mkdir/#findComment-1187740 Share on other sites More sharing options...
Adam Posted March 15, 2011 Share Posted March 15, 2011 It should already be taking the value from the form? $_POST['dirname'] is from the form's input: <input type="text" name="dirname" /> Quote Link to comment https://forums.phpfreaks.com/topic/230693-mkdir/#findComment-1187744 Share on other sites More sharing options...
Append Posted March 15, 2011 Author Share Posted March 15, 2011 It just makes the directory 'to'. Quote Link to comment https://forums.phpfreaks.com/topic/230693-mkdir/#findComment-1187747 Share on other sites More sharing options...
Adam Posted March 15, 2011 Share Posted March 15, 2011 Sorry, there's a typo: if (mkdir($path . $dir)) Should be: if (mkdir($path . $name)) Although you're going to want to change the $path value to the actual path where you want the directories creating. Be sure to end it with a double backslash, otherwise the single backslash will effectively escape the single quote, and you'll have a syntax error. Quote Link to comment https://forums.phpfreaks.com/topic/230693-mkdir/#findComment-1187749 Share on other sites More sharing options...
Append Posted March 15, 2011 Author Share Posted March 15, 2011 That worked, thanks! Just another quesiton, prevents me from making a new thread, is there a way with PHP that I can have a 'copy to' ? So, If I have a file in '../files/file.zip' can I copy it to 'C:\placetocopy\$string from folder just made\file.zip'? Quote Link to comment https://forums.phpfreaks.com/topic/230693-mkdir/#findComment-1187759 Share on other sites More sharing options...
Adam Posted March 15, 2011 Share Posted March 15, 2011 Yup, using the copy() function. I'll let you have a bash at implementing that though.. Quote Link to comment https://forums.phpfreaks.com/topic/230693-mkdir/#findComment-1187761 Share on other sites More sharing options...
Append Posted March 15, 2011 Author Share Posted March 15, 2011 I copied it to the other directory (In C:\), but I don't know how or what functions to use in order to get it to move to the exact folder, or extract to the folder. Quote Link to comment https://forums.phpfreaks.com/topic/230693-mkdir/#findComment-1187781 Share on other sites More sharing options...
Adam Posted March 15, 2011 Share Posted March 15, 2011 What do you mean? Whatever path/name/extension you provide in the second argument within the call to copy(), is what the file will be saved as. Quote Link to comment https://forums.phpfreaks.com/topic/230693-mkdir/#findComment-1187784 Share on other sites More sharing options...
Append Posted March 15, 2011 Author Share Posted March 15, 2011 $file = 'directory/zipfile.zip'; $newfile = 'C:\directory2\zipfile.zip'; if (!copy($file, $newfile)) { echo "Failed $file...\n"; } Right now i have this But I want to be able to make the $newfile location to go to the file we just made (the one that's imputted into the textbox) Quote Link to comment https://forums.phpfreaks.com/topic/230693-mkdir/#findComment-1187788 Share on other sites More sharing options...
Adam Posted March 15, 2011 Share Posted March 15, 2011 You have the new path stored within $path and $name. You just need to concatenate them and add the file name to the end: $copy_to = $path . $name . '\zipfile.zip'; Quote Link to comment https://forums.phpfreaks.com/topic/230693-mkdir/#findComment-1187790 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.