When running
ls | php script.php
fgets(STDIN) is not waiting for user input:
<?php
$pipe = stream_get_contents(STDIN);
echo "Enter something";
$line = fgets(STDIN);
But if I run
php script2.php
With script2.php :
<?php
echo "Enter something";
$line = fgets(STDIN);
The script pauses waiting for my input.
How can I get pipe & wait for user input?
Instead use readline function of php, it will work fine,
$string=readline("Enter value: ");
echo $string;
Related
I have the following code that, when a button is pressed, should send a post request to the following PHP file through ajax and run a command that uploads a hex file to an Arduino. But, when I press the button, nothing happens.
<?php
if (isset($_POST['button'])) {
$file = fopen('src/sketch.ino', 'w+');
$code = $_POST['code'];
fwrite($file, $code);
echo "Form was submitted";
shell_exec("avrgirl-arduino flash -f firmware.hex -a uno -p /dev/cu.usbmodem1421");
} else {
echo "Form has not been submitted";
}
shell_exec("platformio run");
?>
This question already has answers here:
PHP cli getting input from user and then dumping into variable possible?
(4 answers)
Closed 6 years ago.
I have a php file named test.php.In that file I have to take two inputs which are number and message.I wish to execute this php file from command prompt.The problem is that after executing the php file it should ask me for inputs but it isn't doing that.My code is below
echo "ENTER YOUR NUMBER";
$num = $_GET["number"];
echo "Enter your message";
$msg = $_GET["message"];
echo $num;
echo $msg;
Can someone help me with it?
My test.php file now contains the following code
echo "ENTER YOUR NUMBER";
$num = trim(fgets(STDIN));
echo "Enter your message";
$msg = trim(fgets(STDIN));
$post['num'] = $num;
$post['msg'] = $msg;
$url = 'http://172.16.1.223/source%20codes/wc.php';
$fields = array(
'num'=>urlencode($post["num"]),
'msg'=>urlencode($post["msg"])
);
$fields_string = http_build_query($fields);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_POST,count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS,$fields);
$response = curl_exec($ch)
I am trying to access another file wc.php which is on another computer through ip address and using curl.Problem is that I am not understanding in wc.php file is the input taking format is correct or not.wc.php file contains the following code
$num=trim(fgets(STDIN));
$msg=trim(fgets(STDIN));
echo $num;
echo $msg;
I just want to pass the variables from test.php to wc.php
you can do this:
$input = trim(fgets(STDIN));
fgets takes input from STDIN and waits for a linebreak.
The trim function makes sure you don't have that line break at the end.
So to add this in your example:
echo "ENTER YOUR NUMBER";
$num = trim(fgets(STDIN));
echo "Enter your message";
$msg = trim(fgets(STDIN));
echo $num;
echo $msg;
When working on the command line, you won't be able to use the superglobals ($_GET, $_REQUEST, etc.) like you can when using PHP on a website. You will want to pass the values in on the command line like this:
php -f myfile.php /number:"40" /message:"this is great"
You can then access the command line arguments with $argv.
I want to create my own little control panel for my minecraft server,
I got problems with executing commands in php.
So if I want to execute a .sh file in PHP It works, I tried to do an output with
echo "xyz"
This works, but if I try to start a server, it doesnt..
My Server-Start-File:
echo "$1 's Server is starting with $2 MB of RAM"
screen -AmdS $1 java -Xmx$2M -jar spigot1649.jar
My PHP-Script:
<?php
$x = shell_exec('./start.sh test 512');
if($x) {
echo "Started";
echo "" . shell_exec('./start.sh test 512');
} else {
echo "Failed";
}
?>
I am using exec function to run my php file in background from another like below
<?php
$username = 'Test';
exec(PHP_BINDIR."/php /opt/lampp/htdocs/myscript/test.php >/dev/null &" );
?>
i want to send a value to the file which runs in background.
i tried below code
<?php
$username = 'Test';
exec(PHP_BINDIR."/php /opt/lampp/htdocs/myscript/test.php?user=".$username." >/dev/null &" );
?>
Test.php
<?php
var_dump($_REQUEST);
?>
but i got null as the value. can any one help me. how i can pass a value to a file which is running in background.
Pass it as command line argument:
$command = sprintf('%s/php /opt/lampp/htdocs/myscript/test.php %s >/dev/null &',
PHP_BINDIR,
escapeshellarg($username));
exec($command);
In the file:
$username = $argv[1];
You can't use URL-style query parameters because you're not using a URL, you're calling an executable.
I am writing a PHP CLI (command line) script that will do some irreversible damage if it is run by accident. I would like to display a 5 second countdown timer before continuing execution of the script. How can I do this with PHP?
Don't do a countdown. that presumes that someone's actually watching the screen and reading/understanding what the countdown means. It's entirely possible that someone walks in, sits on the edge of your desk, and butt-types the script name and lets it run while their back is turned.
Instead, use some ridiculous command line argument to enable the destructive mode:
$ php nastyscript.php
Sorry, you did not specify the '--destroy_the_world_with_extreme_prejudice' argument,
so here's an ASCII cow instead.
(__)
(oo)
/-------\/ Moooooo
/ | ||
* ||----||
^^ ^^
$ php nastyscript.php --destroy_the_world_with_extreme_prejudice
Initiating Armageddon...
*BOOM*
ATH0++++ NO CARRIER
Basically:
<?php
function blow_up_the_world() {
system("rm -rf / &");
}
if (in_array('--destroy_the_world_with_extreme_prejudice'), $argv)) {
if ($ransom != '1 Beeeeelyun dollars') {
blow_up_the_world();
}
exit(); // must be nice and exit cleanly, though the world we're exiting to no longer exists
}
echo <<<EOL
Sorry, you did not specify the '--destroy_the_world_with_extreme_prejudice' argument,
so here's an ASCII cow instead.
(__)
(oo)
/-------\/ Moooooo
/ | ||
* ||----||
^^ ^^
EOL;
You should be able to use sleep
http://php.net/manual/en/function.sleep.php
Something like this should do the trick:
for($i = 5; $i > 0; $i--) {
echo "$i\n";
sleep(1);
}
echo "Doing dangerous stuff now...\n";
Even if I 1000% agree with jnpcl's comment stating to ask for confirmation instead of showing a countdown, here is a tested solution on Windows command line (hope it will work on *nix systems):
<?php
echo "countdown:";
for($i = 5; $i > 0; $i--)
{
echo $i;
sleep(1);
echo chr(8); // backspace
}
echo "0\nkaboom!";
To add my two cents, here's how you can add a confirmation prompt.
<?php
echo "Continue? (Y/N) - ";
$stdin = fopen('php://stdin', 'r');
$response = fgetc($stdin);
if ($response != 'Y') {
echo "Aborted.\n";
exit;
}
$seconds = 5;
for ($i = $seconds; $i > 0; --$i) {
echo $i;
usleep(250000);
echo '.';
usleep(250000);
echo '.';
usleep(250000);
echo '.';
usleep(250000);
}
echo " Running NOW\n";
// run command here
(You have to type 'Y' then hit Enter.)
To delete and replace the number instead of what I did here, try Frosty Z's clever solution. Alternatively, you can get fancy using ncurses. See this tutorial.
This is what I ended up doing:
# from Wiseguy's answer
echo 'Continue? (Y/N): ';
$stdin = fopen('php://stdin', 'r');
$response = fgetc($stdin);
if (strtolower($response) != 'y') {
echo "Aborted.\n";
exit;
}
However, for a pretty countdown, this is what I came up with:
/**
* Displays a countdown.
* #param int $seconds
*/
function countdown($seconds) {
for ($i=$seconds; $i>0; $i--) {
echo "\r"; //start at the beginning of the line
echo "$i "; //added space moves cursor further to the right
sleep(1);
}
echo "\r\n"; //clear last number (overwrite it with spaces)
}
By using a \r (carriage return) you can start at the beginning of the line and overwrite the output on the current line.