-
Notifications
You must be signed in to change notification settings - Fork 0
/
php.txt
43 lines (36 loc) · 1.23 KB
/
php.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
ini_set('display_errors', '1');
error_reporting(-1); // report everything, forever
# echo CLI errors to STDERR
fwrite(STDERR, "E: invalid input!");
# for PHP's own errors (not tested):
ini_set('display_errors', 'stderr');
# Detect whether input or output are tty. if not, it's likely pipes.
echo "input: ".(posix_isatty(STDIN) ? 'tty' : 'no tty')."\\n";
echo "output: ".(posix_isatty(STDOUT) ? 'tty' : 'no tty')."\\n";
# prompt reader for value from CLI script:
function input(string $prompt = null): string
{
echo $prompt;
$handle = fopen ("php://stdin","r");
$output = fgets ($handle);
return trim ($output);
}
# call nano from PHP CLI script, for a temp file, say
$fname = tempnam('/tmp', '');
file_put_contents($fname, "some value or variable or other file content...");
// edit the temp file, loop back if the saved content is not valid
while (1) {
system("nano $fname > $(tty)");
$content = file_get_contents($fname);
// check content..
if ($error) {
$again = input("there were errors, want to try again? "); // input() see above
if (! in_array($again, [yes, y, etc])) {
break;
}
}else{
file_put_contents($realFile, $content);
break;
}
}
unlink($fname);