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 Link to comment https://forums.phpfreaks.com/topic/106511-check-file/ 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? Link to comment https://forums.phpfreaks.com/topic/106511-check-file/#findComment-545970 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 Link to comment https://forums.phpfreaks.com/topic/106511-check-file/#findComment-545971 Share on other sites More sharing options...
paulman888888 Posted May 20, 2008 Author Share Posted May 20, 2008 Thankyou Link to comment https://forums.phpfreaks.com/topic/106511-check-file/#findComment-545973 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? Link to comment https://forums.phpfreaks.com/topic/106511-check-file/#findComment-545974 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. Link to comment https://forums.phpfreaks.com/topic/106511-check-file/#findComment-545975 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 Link to comment https://forums.phpfreaks.com/topic/106511-check-file/#findComment-545984 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. Link to comment https://forums.phpfreaks.com/topic/106511-check-file/#findComment-546471 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.