Forgive me, I'm not a programmer.
I am trying to include the last lines of a log file on a PHP page with two line breaks after each line read. Using responses from other questions, I came up with the following:
$file = "error.log";
foreach(file($file) as $line) {
echo -e $line. "\n\n";
}
When I execute this on the command line using php file.php, it is displayed correctly with the line breaks, however, on a webpage, it ignores the line breaks and prints a dirty output.
I tried to use echo -e in place of echo, but that doesn't even present the dirty output, but instead throws an error:
PHP Parse error: syntax error, unexpected '$line' (T_VARIABLE), expecting ',' or ';' in /var/www/err.php on line 12
I'm assuming echo -e isn't allowed within the foreach function? What am I doing wrong and how can I achieve the desired results?
echo in PHP is different than the echo command line in linux. You can't pass arguments to echo in PHP like that.
try :
echo nl2br($line . PHP_EOL);
Or if you only care about the browser
echo $line . "<br>";
Related
I'm trying to run a cat command using the shell_exec function, to be more precise something like this:
cat <(echo "foo") bar.xml > foo-bar.xml
But I'm getting a syntax error like the following one:
sh: 1: Syntax error: "(" unexpected
I'm completely lost since this works fine locally and when the command is executed manually in the server, but when running the php script it returns the syntax error. Any clues?
Original code being used:
$shell_cmd = "cat <(echo \"{$this->xmlHeader}\") ";
$shell_cmd .= '\'' . $path . $filename . '\'' . " ";
$shell_cmd .= " > " . '\'' . $path . "hfb/" . strtolower(str_replace($this->replace_values, '', $hfbName)) . ".xml" . '\'';
shell_exec($shell_cmd);
The problem here is likely to be which shell is used. It's not really documented, but I believe shell_exec will use /bin/sh, which will often be a minimal Posix-compliant shell (or a more complex shell emulating that compliance). That's very useful, because it means system scripts will always work the same way, regardless of what other shells are installed or configured for particular users.
When you log in directly, however, you're probably using bash, which has extra features such as the <(...) syntax for getting a named file descriptor from a command.
The best approach in this case is to make your command use only standardised facilities, which will be available in /bin/sh.
In particular, you are using cat to glue together a literal string from echo and a file:
cat <(echo "foo") bar.xml
That can be expressed instead by first echoing the string, and then outputting the file:
echo "foo"; cat bar.xml
To gather both into one output, place them in braces:
{ echo "foo"; cat bar.xml; } > foo-bar.xml
Alternatively, you can give cat an argument of - to concatenate standard input with one or more other files, so you could pipe the value from echo into it:
echo "foo" | cat - bar.xml > foo-bar.xml
I am attempting to write some tests for an email parser I am building and having trouble getting started.
For normal operation an email will be piped to the script, but for the tests I want to simulate the piping action : )
My test is starting out like this:
#!/opt/php70/bin/php
<?php
define('INC_ROOT', dirname(__DIR__));
$script = INC_ROOT . '/app/email_parser.php';
//$email = file_get_contents(INC_ROOT . '/tests/test_emails/test.email');
$email = INC_ROOT . '/tests/test_emails/test.email';
passthru("{$script}<<<{$email}");
With the script as is, the only thing passed to stdin is the path to the test email. When using file_get_contents I get:
sh: -c: line 0: syntax error near unexpected token '('
sh: -c: line 0: /myscriptpath/app/email_parser.php<<<TestEmailContents
Where TestEmailContents is the contents of the raw email file. I feel like I have executed scripts in this manner in the past using the heredoc operator to pass data into stdin. But for the last few days I have been unable to find any information to get me past this stumbling block. Any advice will be mucho appreciado!
The syntax error experienced was exactly that. To get the file contents and pass it in as a here string I needed to single quote the string:
$email = file_get_contents(INC_ROOT . '/tests/test_emails/test.email');
passthru("{$script} <<< '{$email}'");
But, in my case passing in a raw email did not require the use of a here string. The line endings are preserved either way. Redirecting the file to the script yielded the same results.
$email = INC_ROOT . '/tests/test_emails/test.email';
passthru("{$script} < {$email}");
To read stdin in PHP you can use php://stdin filename: $content = file_get_contents('php://stdin'); or $f = fopen('php://stdin', 'r');.
To pass a string to an invoked process you have two options: popen or proc_open. The popen function is easier to use, but it has limited use. The proc_open is a bit more complicated, but gives you much finer control of stdio redirection.
Both function give you file handle(s) on which you can use fwrite and fread. In your case the popen should be good enough (simplified):
$f = popen('./script.php', 'w');
fwrite($f, file_get_contents('test.email'));
pclose($f);
I have recently put up a server, and I am making changes gradually. One of the changes is the contents of a php page. I commonly use a php "shell" (just a textarea and the php just evals what is written). I have made some changes and want to use
fwrite($file, "<?php php here ?>");
but i am getting an error. I believe I know the problem, and it is because i have nested php like
<?php
$a = fopen('../php.php', 'w');
fwrite($a, "
<?php
eval($_POST['phptorun']);
?>
");
fclose($a);
?>
Is there any way I can get it to just put that php code into the file php.php?
The error i was getting is:
Parse error: syntax error, unexpected '' (T_ENCAPSED_AND_WHITESPACE), expecting identifier (T_STRING) or variable (T_VARIABLE) or number (T_NUM_STRING) in /var/www/html/Tyler/replace.php on line 5
Edit:
I forgot to say a couple of things. The server is a LAMP (Linux Apache MySQL PHP), running on my Raspberry Pi 2, model B. If you want to visit my server, it is at 71.204.114.18
Try to escape the $ symbol:
<?php
$a = fopen('../php.php', 'w');
fwrite($a, "
<?php
eval(\$_POST['phptorun']);
?>
");
fclose($a);
?>
I’m writing a very simply php script that take a path to a command line argument and prints it out.
Here is my code:
<?php
$argument1 = $argv[1];
$argument2 = $argv[2];
echo $argv[1];
?>
Output:
user-mbp:desktop$ php test_script.php /desktop/my/path/
/desktop/my/path/user-mbp:desktop user$
You can see the result "/desktop/my/path/" is behind the line of user-mbp:desktop $
But I want the result is like:
/desktop/my/path/ <- Line 1
user-mbp:desktop user$ <- Line 2
Do you have any hint for my code? I’m appreciated it.
I tried the command echo $argv[1] ."\n", but the result is weird:
/desktop/my/path/
user-mbp:desktop user$
You can see is does show under the line of user.
But I don't quite understand why the line user-mbp:desktop user$ shifts and goes to the next line! Thanks again!
For command line scripts, you're going to always need to add a newline at the end of your echo'd content. So, this:
echo $argv[1] . "\n";
Your shell won't do that for you automatically, and PHP won't do it automatically because it shouldn't do it automatically, and you didn't explicitly tell it to.
I have a small foreach loop in php but the results will not pass to a shell script. see below:
$contents = file("$target_dir/$user.temp.txt");
foreach($contents as $line) {
echo $line . "<br />";
exec("sh read.sh $line >> tempfile");
}
the echo statement works just fine and displays the data to screen as it should. however the result of $line does not make it to the shell script, but when I replace $line with a random string it does. here is the shell script:
#!/bin/bash
#test script
echo "test output: $1"
when trying to call the shell script with $line in place, the tempfile will get created but is blank. all my permissions are set to 777 and the group calling the script is the same owner as the folder. I've reviewed other posts about php loops but dont seem to find anything that matches exactly what my issue is.
Thanks in advance for any insight.
Needed to add single quotes to $line.
exec("sh read.sh '$line' >> tempfile");