harvey Posted December 12, 2007 Share Posted December 12, 2007 I have a php script that processes the output from a form. Part of the processing involves writing to a file. I'd like to ask the user the standard question: "This file exists. Do you want to overwrite it? : Quote Link to comment Share on other sites More sharing options...
marcus Posted December 12, 2007 Share Posted December 12, 2007 <?php if(file_exists("/path/to/the/file.txt")){ echo "The file exists, would you like to overwrite it?\n"; }else { echo "File doesn't exist, would you like to create it?\n"; } ?> eh? Quote Link to comment Share on other sites More sharing options...
harvey Posted December 12, 2007 Author Share Posted December 12, 2007 While your solution prints a question to the screen, I don't see how I gather the user response. Quote Link to comment Share on other sites More sharing options...
lemmin Posted December 12, 2007 Share Posted December 12, 2007 You could use a form: //modification of mgallforever's code <?php echo "<form method=POST action=\"writetofile.php\">"; if(file_exists("/path/to/the/file.txt")){ echo "The file exists, would you like to overwrite it?<br> <input type=submit name=\"overwrite\" value=\"yes\"> <input type=submit name=\"overwrite\" value=\"no\">\n"; }else { echo "File doesn't exist, would you like to create it?<bR> <input type=submit name=\"create\" value=\"yes\"> <input type=submit name=\"create\" value=\"no\">\n"; } echo "</form>"; ?> Quote Link to comment Share on other sites More sharing options...
harvey Posted December 12, 2007 Author Share Posted December 12, 2007 OK, but this is getting a bit more complicated than I might wish. For now I have to pass to yet another page, writetofile.php not only the yes/no options, but also the filepath, and the content to be written. Quote Link to comment Share on other sites More sharing options...
lemmin Posted December 12, 2007 Share Posted December 12, 2007 You can do everything in one file. Just use if (isset($_POST['overwrite'])) To see if it is currently getting the overwrite answer. If it isn't it should be checking if the file exists. I have written a number of applications in a single php file. There is no way to get a user event in the middle of a php script running so there is no other way than to pass it to the file one more time. Quote Link to comment Share on other sites More sharing options...
harvey Posted December 12, 2007 Author Share Posted December 12, 2007 Hmm. I'm still not quite getting it (but appreciate your help). If I understand, your suggestion is something like: step 1. originalform.php --> (post filename, contents) --> process.php step 2. process.php --> (post answer to yes/no confirmation) --> process.php Now process.php can test for isset($_POST['overwrite']). Let's say the value is 'yes'. However, on the second call (to itself) it has lost the values to, $_POST['filename'] and $_POST['contents']. Or is your suggestion that originalform.php is the only file I need?? (Sorry to be so dense.) Perhaps if you have a small (but complete) toy example? Harvey Quote Link to comment Share on other sites More sharing options...
lemmin Posted December 12, 2007 Share Posted December 12, 2007 In the <form> that has the overwrite button, include a hidden field with the other values: //modification of mgallforever's code <?php echo "<form method=POST action=\"writetofile.php\">"; if(file_exists("/path/to/the/file.txt")){ echo "The file exists, would you like to overwrite it?<br> <input type=submit name=\"overwrite\" value=\"yes\"> <input type=submit name=\"overwrite\" value=\"no\">\n"; }else { echo "File doesn't exist, would you like to create it?<bR> <input type=submit name=\"create\" value=\"yes\"> <input type=submit name=\"create\" value=\"no\">\n"; } echo <input type=HIDDEN name=\"filename\" value=\"" . $_POST['filename'] . "\"> <input type=HIDDEN name=\"contents\" value=\"" . $_POST['contents'] . "\">"; echo "</form>"; ?> Then those two values won't be lost. Quote Link to comment Share on other sites More sharing options...
harvey Posted December 12, 2007 Author Share Posted December 12, 2007 Seems to work! Thanks much for your help. Harvey 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.