Show errors or console file lua in editor php - php

I use PHP for edit code LUA, change and save file. I can execute the file, but I can not use show if I have error, as console.
example:
my code lua:
print "hello"
and code php:
<?php
$output = passthru("sudo /usr/local/bin/lua /var/www/test.lua");
//show output
echo "<pre>$output</pre>";
?>
this work, but I can show errors (console), if code has.
i think use the code io.stdin or io.stdout in lua
thanks

See this comment: http://php.net/manual/en/function.passthru.php#101148
When Lua fails and responds with an error, passthrough can't capture the result, even though I get the STDERR text without any problems, you may, as suggested in the comment, try piping the Lua result to tee program.
$output = passthru("sudo /usr/local/bin/lua /var/www/test.lua | tee");

Related

PHP shell_exec command doesn't work

I am trying learn to execute shell scripts from within PHP code. So, I made a test program to execute a bash script from within PHP. However, it has no effect. The relevant code is shown below.
<?php
.......
shell_exec('/bin/bash /var/www/html/just_touch.sh');
?>
The just_touch.sh script just creates a new file, like as shown below.
touch /home/user/some.txt
I was expecting to have file /home/user/some.txt after execution, but no, it isn't made. What mistake, am I doing?
P.S: The following code works though.
$output = shell_exec('ls /home/user');
echo $output;
Does this have anything to do with permissions?
Moreover, I notice that while this prints "Can you see me?".
$output = shell_exec('echo Can you see me?');
echo $output;
This doesn't!
shell_exec('echo Can you see me?')
What is going on here?
Stderr is lost when using shell_exec. You might wan't to use:
shell_exec('/bin/bash /var/www/html/just_touch.sh 2>&1');

Live output of shell script using `watch` in browser?

I've seen PHP reading shell_exec live output and PHP: Outputting the system / Shell_exec command output in web browser, but can't get the following working.
NB: originally, my shell script was running some python, but I have simplified it.
live.sh
uname -a
date
watch --no-title date
live.php
<?php
$cmd = "sh live.sh";
while (# ob_end_flush()); // end all output buffers if any
$proc = popen($cmd, 'r');
echo '<pre>';
while (!feof($proc))
{
echo fread($proc, 4096);
# flush();
}
echo '</pre>';
?>
The uname and date outputs appear in the browser okay, but the watch output does not.
Am I in fact attempting the impossible?
I would advise against the approach of using watch, and you are probably in for more trouble than you expect.
Firstly, long running command might be affected by the (default) PHP timeout, so you may have to tweak that.
Then, watch probably uses terminal sequences to clear the screen, and I am not sure how this translates to the output code.
I would rather suggest setting up a client side mechanism to periodically repeat a call to the sever side live.php.
This post on SO will help you get started.
jQuery, simple polling example
the page above uses the jquery library, but you could use native Javascript equivalent if you want to.
The simplest example (from that page) would be :
function poll(){
$("live.php", function(data){
$("#output_container").html(data);
});
}
setInterval(function(){ poll(); }, 5000);
In your page, set up a container for your results
<div id="output_container"></div>
In your example, you remove the watch from your script and replace it with the command you intended watching.
You probably want this only for some special thing. So, for those you can try ajaxterm.
Really easy to use, (4.line installation)
wget http://antony.lesuisse.org/software/ajaxterm/files/Ajaxterm-0.10.tar.gz
tar zxvf Ajaxterm-0.10.tar.gz
cd Ajaxterm-0.10
./ajaxterm.py
and you will get full bash running interactively in the browser. (after the login/password). Written in python, so easily adaptible for you. Also, see here.

retrieve R output in PHP

Here's the issue:
I am using R to run some statistical analysis. The results of which will eventually be sent to a an embedded swf on the user's client machine.
To do this, I have PHP execute a shell script to run the R program, and I want to retrieve the results of that program so I can parse them in PHP and respond with the appropriate data.
So, it's simply:
$output = shell_exec("R CMD BATCH /home/bitnami/r_script.R");
echo $output;
But, I receive nothing of course, because R CMD BATCH writes to a file. I've tried redirecting the output in a manner similar to this question which changes my script to
$output = shell_exec('R CMD BATCH /home/bitnami/raschPL.R /dev/tty');
echo $output;
But what I get on the console is a huge spillout of the source code, and nothing is echoed.
I've also tried this question's solution in my R script.
tl;dr; I need to retrieve the results of an R script in PHP.
Cheers!
If it writes to file perhaps you could use file_get_contents to read it?
http://php.net/manual/en/function.file-get-contents.php
Found it, the answer is through Rscript. Rscript should be included in the latest install of R.
Using my code as an example, I would enter this at the very top of r_script.R
#!/usr/bin/Rscript --options-you-need
This should be the path to your Rscript executable. This can be found easily by typing
which Rscript
in the terminal. Where I have --options-you-need, place the options you would normally have when doing the CMD BATCH, such as --slave to remove extraneous output.
You should now be able to run your script like so:
./r_script.R arg1 arg2
Important! If you get the error
Error in `contrasts<-`(`*tmp*`, value = "contr.treatment") :
could not find function "is"
You need to include the "methods" package, like so:
require("methods");
Perhaps,a much simpler workaround, would be:
$output = shell_exec('R CMD BATCH /home/bitnami/raschPL.R > /dev/tty 2>&1');
echo $output;
Redirects both STDOUT and STDERR, since R outputs to STDERR, by default.

stop php displaying to screen - run silent

I have a php page that when called from a browser displays a page, it also generates a static html page so - http://somewebsite/create.php when run from a browser creates newpage.html.
I also run this page from the CLI #php create.php it creates newpage.html but I get all the output on the screen, this slows down execution and if I have to run it many times can take hours.
Is there a way to run #php create.php and to supress all output to the screen
MArtyn
Check out Output Buffering
You begin with ob_start() to start buffering output, then you can use $output = ob_get_clean() to get everything that would have been displayed and store it in $output, so you can do whatever you want with it.
I believe there are $_SERVER variables you can use to check if you're running from the command line or not. (Obviously you can check for the HTTP headers that are normally there, they will not be set from the CLI)
var_dump($_SERVER) and see if there's anything intuitive looking :)
You can pipe the output on the command-line to /dev/null such as:
php http://somewebsite/create.php > /dev/null
This doesn't actually stop output from being produced by php, only from being displayed.
If you actually want to stop the output from being produced, you could use php's output buffering with ob_start() and put the output into a variable with ob_get_flush() at the end of the code and only echo the output if a certain param is/is not passed to the script.

Calling php from php through exec() gives no result

I have a PHP script that creates other PHP files based on user input. Basically, there are files containing language specific constants (define) that can be translated by the user. In order to avoid runtime errors, I want to test newly written files for parse errors (due to "unusual" character sequences). I have read several posts here on SO (like PHP include files with parse errors) and tried a function that uses
$output = exec("php -l $filename");
to determine whether a file parses correctly. This works perfectly on my local machine, but at on the provider's machine, the output of calls to exec("php ...") seems to be always empty. I tried a call to ls and it gives me output, leading me to the assumption that PHP is somehow configured to not react to command line invocations or so. Does anyone know a way around this?
EDIT: I forgot to mention, I had already tried shell_exec and it gives no result, either. In response to sganesh's answer: I had tried that too, sorry I forgot to mention. However, the output (second argument) will always be an empty array, and the return value will always be 127, no matter if the PHP file to test has syntax errors or not.
I had the same problem. The solution that worked for me was found in running-at-from-php-gives-no-output. I needed to add output redirection.
$output = exec("php -l $filename 2>&1");
You can try with exec second and third arguments.
second argument will have the output of the command.
third argument will have the return value.
And exec will return only last line of the command.
$filename = "a.php";
$output = exec("php -l $filename",$op,$ret_val);
print $output."\n";
print $ret_val."\n";
var_dump($op);
By executing shell_exec(), you can see the output as if you executed that file via command line. You can just see if there is an error right here.
<?php
if (strpos(shell_exec('php -l file.php'), 'Syntax Error')) {
die('An error!');
}
There may also be a possibility that shell_exec() or exec() may be disable by your host.
Nice idea to check the file validity :-)!
Now, from the PHP manual for exec():
Note: When safe mode is enabled, you can only execute files within the safe_mode_exec_dir. For practical reasons, it is currently not allowed to have components in the path to the executable.
Can you check if this is not the case for you?
Also, can you check by providing the full path of the PHP interpreter in the exec() instead of only php. Let me know how you fare.
Pinaki
the correct way is to add >2&1 as tested on a windows system using imagemagick!
I worked around my original problem by using a different method. Here is what I do now:
Write a temporary file with contents <?php include "< File to test >"; echo "OK"; ?>
Generate the correct URL for the temporary file
Perform HTTP request with this URL
Check if result equals "OK". If yes, the file to test parses without errors.
Delete temporary file
Maybe this could be done without the temporary file by issuing an HTTP request to the file to test directly. However, if there is a parse error and errors are suppressed, the output will be empty and not discernible from the output in the case of a file that gives no parse errors. This method is risky because the file is actually executed instead of just checked. In my case, there is only a limited number of users who have access to this functionality in the first place. Still, I'm naturally not entirely happy with it.
Why the exec() approach did not work, I still do not know exactly. pinaki might be right by suggesting to provide the full path to the PHP executable, but I cannot find out the full path.
Thank you everyone for answering, I upvoted you all. However, I cannot accept any of your answers as none of your suggestions really solved my problem.

Categories