canadezo Posted June 6, 2011 Share Posted June 6, 2011 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"; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/238516-php-file-directory-creation/ Share on other sites More sharing options...
mgoodman Posted June 6, 2011 Share Posted June 6, 2011 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? Quote Link to comment https://forums.phpfreaks.com/topic/238516-php-file-directory-creation/#findComment-1225699 Share on other sites More sharing options...
canadezo Posted June 6, 2011 Author Share Posted June 6, 2011 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> Quote Link to comment https://forums.phpfreaks.com/topic/238516-php-file-directory-creation/#findComment-1225703 Share on other sites More sharing options...
mgoodman Posted June 6, 2011 Share Posted June 6, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/238516-php-file-directory-creation/#findComment-1225712 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.