Jump to content

PHP File & directory creation


canadezo

Recommended Posts

So far everything is working swell; except for the file creation. What the website is going through is the user inputs their name and a directory is created for them. Now; the file creation thing is what's messing me up. As soon as they create that new directory I would like to appoint an index.html to it... This is what I got so far; let me know where I messed up, please & thank you! =)

<?php
if (isset($_POST['create'])) {
$file = 'index.html';
$newfile = ($dirName . 'index.html');

if (!copy($file, $newfile)) {
    echo "failed to copy $file...\n";
}
}
?>

Link to comment
https://forums.phpfreaks.com/topic/238516-php-file-directory-creation/
Share on other sites

It's hard to tell with that tiny bit of code you've provided. It would be nice to know what $dirName is set to. It would also be nice to know what sort of problem you are having. You just said that "file creation is what's messing [you] up".

 

What sort of problems are you having and can you post the code that defines $dirName? Also, have you set the working directory to the correct one for the index.html file you're trying to copy?

I'll post the entire page that I'm working on, and the index file I'm copying is from the same directory as this file; so I assumed that should work, correct me if I'm wrong.

<?php
// set our absolute path to the directories will be created in:
$path = $_SERVER['DOCUMENT_ROOT'] . '/albums/';


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}.";
}
}
?>



<?php
if (isset($_POST['create'])) {
$file = 'index.html';
$newfile = ($dirName . 'index.html');

if (!copy($file, $newfile)) {
    echo "failed to copy $file...\n";
}
}
?>




<html>
<head><title>Make Directory</title>
<style type="text/css">
a:link {
COLOR: #fff;
}
a:visited {
COLOR: #fff;
}
a:hover {
COLOR: #fff;
}
a:active {
COLOR: #fff;
}
</style>
<script type="text/javascript">
function delayedRedirect(){
    window.location = "/default.aspx"
}
</script>
</head>
<body text="#FFFFFF">
<center><?php echo (isset($success)?"<h3>$success</h3>":""); ?>
<h2><font color="black">Add your name & pics</font></h2>
<?php echo (isset($error)?'<span style="color:red;">' . $error . '</span>':''); ?>
<form name="phpMkDIRForm" method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>" >
<font color="black">Enter Your Name (No Spaces or Symbols):<BR> <input type="text" value="" name="dirName" /><br />
<input type="submit" name="create" value="Add Pics" />
</form></center>
</body></font>
</html>

Archived

This topic is now archived and is closed to further replies.

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