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
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?

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

I see a few problems. You might not have a / on the end of $dirName. You also have "!$path" in there, which which would return false. Without looking at the PHP manual I'm pretty sure that the first argument for mkdir is the path, not a boolean value.

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.