Jump to content

Make a subfolder


QuizToon

Recommended Posts

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

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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