So I have this code and I'm only trying to make a list of the saves in another directory where the php scrip is in xampp folder and the saves are to this path /root/files/saves:
<html>
<body>
<?php
$output = shell_exec('ls /root/files/saves');
echo "<pre>$output</pre>";
?>
</body>
</html>
I don't know why I can't get it working on a var_dump it seems output is null I'm really confuse it should work or I just it all wrong I need some help.
Add 2>&1 to the end of your shell command to have STDERR returned as well as STDOUT.
$output = shell_exec("ls /root/files/saves 2>&1");
Also, if the user running PHP doesn't have sufficient permissions to view the output in /root/, the above code will return a Permission denied error message.
Source: http://php.net/manual/en/function.shell-exec.php#28994
Related
I've moved my code form one WAMP computer to another and the code that runs pdftk stopped working. I've compared the permissions on pdftk.exe and they are the same on both machines. When I run the same command from a command prompt, it works. I add exec("whoami") to the script and the user is the same on both computers. When I run something like exec('dir 2>&1', $out) it executes so I know exec is working form within php.
I've created a trivial php file to test and it doesn't work.
<?php
$String = 'pdftk.exe > "c:\temp\temp.txt"';
exec("$String");
exec("pdftk.exe > \"c:\temp\temp.txt\"");
?>
Both exec command result in a 0 byte file being created.
if I run
pdftk.exe > "c:\temp\temp.txt"
from a command line it puts the output of pdftk.exe into the temp.txt file as expected.
This seems like is must be some kind of permissions issue, but permissions on the executable seem to be the same. Losing my mind on this.
In my opinion, first line should be:
<?php
$String = 'pdftk.exe > "c:\temp\temp.txt"';
exec($String); //removed quotes
?>
And for the second line, what if you try this?
<?php
exec('pdftk.exe > "c:\temp\temp.txt\"', $outputAndErrors, $returnValue);
var_dump($outputAndErrors);
?>
Or if you remove your first ">"?
<?php
exec('pdftk.exe "c:\temp\temp.txt\" 2>&1', $outputAndErrors, $returnValue);
var_dump($outputAndErrors);
?>
These are some tests that may help you, thought.
I have a problem when you run a script from php the problem is that it doesn't do what must be done, this is the php code
? php
echo "starting execution";
echo exec("./sc");
?>
and in the code of the script this is sc
mv text.txt /var/www/files/
does not work does not move the file archive there is I've added the chmod permismos but doesn't appreciate any suggestion
First, I would suggest to execute the script file manually from command line(console) and check if the script file is working properly. If that doesn't work, then it won't work in php too.
Try:
echo "starting execution";
echo exec("./sc", $output);
var_dump($output);
contents of sc :
mv text.txt /var/www/files/ 2>&1
If the file is not moved, it must throw an error which you should be able to see in $output.
My guess would be that the error must be related to permission.
I expected this to be quite simple but I cannot make it work.
I'm trying to run a php script in command line (to finally be able to automate it in a cron.)
The file is outside of the public web folder to avoid executing it from a webbrowser.
File is called 'daily.php' and contains :
#!/usr/bin/php
<?
echo "hello \n";
file_put_contents("daily.log", "executed...", FILE_APPEND | LOCK_EX);
?>
then in my terminal I run (as root):
/usr/bin/php daily.php
but it simple outputs me the full source code without the hashbang.
So I tried changing the file to 755, tried to chmod +x it but still outputs me the full source code.
I had a look in the man page and I found :
-f <file> Parse and execute <file>.
Tried this but still the same output.
Why is this? how can I interpret this file?
Ok
I found the answer after 2 hours of trouble, and just after posting this question.
Even if you hashbang the file, even if you call php directly, you need to implicitly tell it is php by using :
<?php
?>
and not
<?
?>
I'm trying to run the following command from a PHP file in a web browser:
exec('festival --tts /var/www/test.txt &');
Unfortunately, nothing happens. I thought of trying 'echo' but there is nothing to return to see if the command is working. I set permissions for test.txt to 777. I also ran the command in a shell and it works just fine - just not when submitted by a PHP script. What am I doing wrong?
Provide the full path to the festival binary, you can find it out on the console with which festival command, then use it in your exec call, like this:
exec('/usr/bin/festival --tts /var/www/test.txt &');
Update:
You need to make sure the folder where you are creating the file has write permission for the user running php which usually is www-data on debian based distros.
If I understand correctly what you're trying to do, you aren't returning the input. The syntax for this is:
exec('command', $output);
where $output is the variable in which the script's output will be stored. Oddly, this output will be returned as an array, so don't forget to implode() it when you're done if you're expecting a string.
Does the festival program print any output? If so, try capturing that. Also check the return value.
exec('/usr/bin/festival --tts /var/www/test.txt &', $output, $return);
Dump the output array, like so:
var_dump($output);
Any non-zero return value usually indicates an error:
echo $return;
I'm trying to run a shell script from a php frontend
heres the shell file (run.sh chmod to 777)
#!/bin/bash
wget -O index.html http://markets.usatoday.com/custom/usatoday-com/html-mktscreener.asp
python hw7-9.py index.html
echo "done";
Heres the php front end
<?php
$output = shell_exec("run.sh");
echo "<pre>$output</pre>";
?>
But it php page doesn't return anything except
<pre></pre>
Have you tried doing error_reporting(-1); at the top of your PHP script. Likely shell_exec is disabled on your server.
Would you try
$output = shell_exec("sh run.sh");
Check what user the php/web server is actually run as - e.g. "www-user" may have no permissions whatsoever to do the things your script is trying to do (and for good reason).