Created
June 21, 2011 04:34
-
-
Save jachin/1037249 to your computer and use it in GitHub Desktop.
PHP Command Line Confirmation Prompt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$message = "Are you sure you want to do this [y/N]"; | |
print $message; | |
flush(); | |
ob_flush(); | |
$confirmation = trim( fgets( STDIN ) ); | |
if ( $confirmation !== 'y' ) { | |
// The user did not say 'y'. | |
exit (0); | |
} |
Use exit(1)
instead of exit(0)
to prevent next process running.
Example command: php confirm-this.php && php run-that.php
.
If enter n then run-that.php will not run because exit(1)
.
Also prevent no buffer to flush error with ob_get_level()
. Example:
if (ob_get_level() > 0) {
ob_flush();
}
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
And here is mine.