paulman888888 Posted May 20, 2008 Share Posted May 20, 2008 This is my code! <?php $ourFileName = "games/{$_POST['name']}.php"; // the name of the file to be made $ourFileHandle = fopen($ourFileName, 'w') or die("Can't make file"); fclose($ourFileHandle); $myFile = "games/{$_POST['name']}.php"; $fh = fopen($myFile, 'w') or die("There's an error!"); $stringData = "$_POST[main].php"; fwrite($fh, $stringData); fclose($fh); ?> I would like something to check if the file name already exists. Can someone please help. Thankyou Quote Link to comment Share on other sites More sharing options...
947740 Posted May 20, 2008 Share Posted May 20, 2008 Does $_POST['name'] already have the extension in it? $_POST[main] should be $_POST['main']. Why do you a file, then close it, and then open the file, just with another variable name? Quote Link to comment Share on other sites More sharing options...
micmania1 Posted May 20, 2008 Share Posted May 20, 2008 try file_exists() http://www.php.net/file_exists Quote Link to comment Share on other sites More sharing options...
paulman888888 Posted May 20, 2008 Author Share Posted May 20, 2008 Thankyou Quote Link to comment Share on other sites More sharing options...
paulman888888 Posted May 20, 2008 Author Share Posted May 20, 2008 whats the differnce between $_POST['name'] and $_POST[name].php? do the ' doing anything? Quote Link to comment Share on other sites More sharing options...
947740 Posted May 20, 2008 Share Posted May 20, 2008 It makes name a string, which allows it to find the value of the variable. Otherwise it does not know it is a string, and it will not return the correct value, if it returns a value at all. Quote Link to comment Share on other sites More sharing options...
paulman888888 Posted May 20, 2008 Author Share Posted May 20, 2008 is this right? <?php $filename = "games/{$_POST['name']}.php"; if (file_exists($filename)) { echo "Theres an error. That file already exists. Go back and change the name of the game."; } else { $ourFileName = "games/{$_POST['name']}.php"; $ourFileHandle = fopen($ourFileName, 'w') or die("Can't make file"); fclose($ourFileHandle); $myFile = "games/{$_POST['name']}.php"; $fh = fopen($myFile, 'w') or die("There's an error!"); $stringData = "$_POST['main'].php"; fwrite($fh, $stringData); fclose($fh); } ?> thankyou Quote Link to comment Share on other sites More sharing options...
947740 Posted May 21, 2008 Share Posted May 21, 2008 It looks fine to me, but I only know a little about file functions. Quote Link to comment 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.