The Create Directory PHP Script
Creating a directory is relatively straight forward using the mkdir() function. It accepts 2 parameters, the first will be the new directory and the second will be the permission level (you can modify this script to take in a permission as well if you like. For this tutorial we will set it to 0775). Using regex we will validate the directory name and using is_dir() function we will verify that the directory does not exist and the name contains only alpha numeric data. As a side note I make use of the Ternary Operator.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 | <?php /********************** File: createDir.php Author: Frost Website: http://www.slunked.com ***********************/ // set our absolute path to the directories will be created in: $path = $_SERVER['DOCUMENT_ROOT'] . '/uploads/'; if (isset($_POST['create'])) { // Grab our form Data $dirName = isset($_POST['dirName'])?$_POST['dirName']:false; // first validate the value: if ($dirName !== false && preg_match('~([^A-Z0-9]+)~i', $dirName, $matches) === 0) { // We have a valid directory: if (!is_dir($path . $dirName)) { // We are good to create this directory: if (mkdir($path . $dirName, 0775)) { $success = "Your directory has been created succesfully!<br /><br />"; }else { $error = "Unable to create dir {$dirName}."; } }else { $error = "Directory {$dirName} already exists."; } }else { // Invalid data, htmlenttie them incase < > were used. $dirName = htmlentities($dirName); $error = "You have invalid values in {$dirName}."; } } ?> <html> <head><title>Make Directory</title></head> <body> <?php echo (isset($success)?"<h3>$success</h3>":""); ?> <h2>Make Directory on Server</h2> <?php echo (isset($error)?'<span style="color:red;">' . $error . '</span>':''); ?> <form name="phpMkDIRForm" method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>"> Enter a Directory Name (Alpha-Numeric only): <input type="text" value="" name="dirName" /><br /> <input type="submit" name="create" value="Create Directory" /> </form> </body> </html> |
As you can see we do multiple checks, and if all the checks pan out we will create the directory, if they do not we will show error messages. Since only one page is really needed for the script the form is combined at the end and messages are added to display success / errors, as this script cannot create a directory that is already there, we do not really have to worry about re-directing to wipe POST data, as even if they refresh they will just get an error message (if you were concerned about that). There we have it, a script to take user input from a form and create a directory with validation checks! As I stated, I would highly recommend applying a Login script and validating a user so you do not allow just anyone to create a directory on your server.
Ending Notes
It is hard to emphasize the importance of requiring a password before using this script in an production enviroment. As such I will take no responsibilty for any destruction that comes as a result of utilizing this script. Use at your own use on your server! Aside from that I hope you have learned an easy way to create a user input / manipulation script. This is hopefully basic enough to understand and implement for other items you may have question on how to do. As always, feel free to use this script, if you do like the Create Directory Using PHP and User Input please provide a link back to this tutorial as that would be appreciated!

Thank you very much. Good Code. I’m happy.
I have the php file working correctly but want to know how to return the results in a new separate window instead of the current window.
@Matt: You would need to use javascript to do that. Given I am not a huge Javascript buff, I would suggest you do a google on Javascript and new windows, which should lead you in the right direction.
its working but i need create a label folder like gmail label in php
Warning: mkdir() [function.mkdir]: No such file or directory in C:\xampp\htdocs\random\createDir.php on line 20
How can I fix this; I’m also getting the error: “Unable to create dir” in red letters… Any idea’s?
I got the same error. why?
how to fix
Warning: mkdir() [function.mkdir]: No such file or directory in /opt/lampp/htdocs/createDir.php on line 20
The error means that you do not have the proper permissions for creating a directory. You need to have write access to the folder where you will be creating the directory out (chmod 0755) and the owner / group needs to coordinate with whatever PHP is being ran as, in most cases it will be something like www-data:www-data.
Hello, first of all thanks for posting this, its exactly what i was looking for.
I was wondering if you or anyone around here may know a way to not only create a directory from a user form, but also auto-generate an index file that would just be a copy of the index file of that parent directory or all other subdirectories? (for instance, i am building a directory browser/file uploader for clients to share files with our company, so each folder has an index that has a styled directory browser with file previews and an upload form instead of seeing the raw directory file list.. i’d like them to be able to create a directory but for the directory to have an index in it that matches the theme of the other directories they are able to browse, if that makes sense)
@angela http://php.net/copy If you need something that is recursive: http://www.php.net/manual/en/function.copy.php#104809