QuizToon Posted January 25, 2010 Share Posted January 25, 2010 Hi all I got the following script off an old post I found. It works very well and creates the subfolder. Is it possible to make it so that I can type my required folder name into a form and have it create the folder with that name? For example I might want to create 2 folders, one called folder1 and the called folder2. Is this possible without having to change the code each time <?php /* wherever this particular script will be installed, I want to create a subfolder */ /* Step 1. I need to know the absolute path to where I am now, ie where this script is running from...*/ $thisdir = getcwd(); /* Step 2. From this folder, I want to create a subfolder called "myfiles". Also, I want to try and make this folder world-writable (CHMOD 0777). Tell me if success or failure... */ if(mkdir($thisdir ."/myfiles" , 0777)) { echo "Directory has been created successfully..."; } else { echo "Failed to create directory..."; } ?> Hope you follow me here Thanks in advance Quote Link to comment https://forums.phpfreaks.com/topic/189786-make-a-subfolder/ Share on other sites More sharing options...
premiso Posted January 25, 2010 Share Posted January 25, 2010 Yep, using POST / GET data from the form. But you will want to verify that the POST / GET data does not contain bad characters for DIRs (such as / or ' \) as they can make a folder not deletable. Quote Link to comment https://forums.phpfreaks.com/topic/189786-make-a-subfolder/#findComment-1001580 Share on other sites More sharing options...
QuizToon Posted January 26, 2010 Author Share Posted January 26, 2010 I dont suppose you could point me in the direction of a tutorial or thread which shws how to do this I am not sure where to start. Many thanks for the reply Quote Link to comment https://forums.phpfreaks.com/topic/189786-make-a-subfolder/#findComment-1002042 Share on other sites More sharing options...
premiso Posted January 26, 2010 Share Posted January 26, 2010 Here is the code, if you want the tutorial go here: Create Directory with PHP and User Input (click to view the tutorial). <?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> Quote Link to comment https://forums.phpfreaks.com/topic/189786-make-a-subfolder/#findComment-1002125 Share on other sites More sharing options...
QuizToon Posted January 27, 2010 Author Share Posted January 27, 2010 Hey thanks for exactly what is required. I also read through the tutorial on your site and it makes the code easier to understand and to see what is happening. I am now going to create a login script for this to hide behind as you suggested. Once again I thank you. Quote Link to comment https://forums.phpfreaks.com/topic/189786-make-a-subfolder/#findComment-1002356 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.