I'm trying to run exec() from my php file.
From cmd I'm typing this: gcc sample.c -o sample2.
Sample.c is located in the same directory with my php file.
How could i do the same from php exec;
it works by putting full path of gcc: exec('C:\MinGW\bin\gcc sample.c -o str1', $output, $return);
the same thing with cmd i just type gcc , not full path.
why is this happening?
try this
<?php
chdir(__DIR__);
exec('gcc sample.c -o sample2', $output, $return);
// Return will return non-zero upon an error
if (!$return) {
echo "failed";
} else {
echo "executed";
}
?>
Related
I tried many solutions but nothing it works :
echo '<pre>';
shell_exec("python /home/folder/python/mapfile_compress.py");
shell_exec("sudo -u wwwexec python escapeshellcommand(/home/folder/python/mapfile_compress.py) $uid");
shell_exec("sudo chmod +x /home/folder/python/mapfile_compress.py");
system("bash /home/folder/python/mapfile_compress.py");
passthru("bash /home/folder/python/mapfile_compress.py");
passthru("/home/folder/python/mapfile_compress.py");
exec("bash /home/folder/python/mapfile_compress.py");
echo '</pre>';
I launched indivdually them but in all cases, Firebug returned : '<pre>'
So I tried this code founded on Stack Overflow :
$command = escapeshellcmd('chmod +x /home/folder/python/mapfile_compress_test.py');
echo $command;
$output = shell_exec($command);
echo $output;
But firebug returned nothing.
My python file begin with #!/usr/bin/env python and if I launch it on server that works !
Do you knwo how can I launch my python file from PHP file ?
chmod will return 0 on success and > 0 on error.
Make sure that the file is able to run by just executing it as the web user. When +x is properly set, you can execute it by just calling $ /path/to/your/file.py, the shebang in the first line in your script #!/usr/bin/env python should define the correct python based on your env.
You can test this by running:
$ /usr/bin/env python /path/to/your/file.py
So check your file permissions to check if the file is executable by the user that runs the php script.
Just to test, you can just print a few lines in your python file
#!/usr/bin/env python
print "test line 1"
print "test line 2"
Then if you have verified permissions and the correct use of python, you can do this in your php.
$command = escapeshellcmd('/path/to/your/file.py');
$output = shell_exec($command); // get all output or use passthrough, exec will only return the last line.
echo "<pre>{$output}</pre>;
At first, Do you have enabled shell_exec/system/passthru commands in php.ini?
shell_exec("python /home/folder/python/mapfile_compress.py");
I think, it could be problem with your $PATH. Try something like: (use full path to python)
shell_exec("/usr/bin/python /home/folder/python/mapfile_compress.py");
shell_exec("/usr/local/bin/python /home/folder/python/mapfile_compress.py");
In my case that's work if I write this code :
$command = escapeshellcmd('python /path/to/your/file.py');
exec($command);
When I run this phpscript in my mac xampp it return successful but not pdf file is generated. But when I run from mac terminal it able to create the pdf file from the docx file.
<?php
$source_file = 'c++ documentation.docx';
$cmd = "unoconv/0.7/bin/unoconv";
$command = sprintf("/Applications/XAMPP/xamppfiles/htdocs/phpdocx/unoconv/0.7/bin/unoconv -h -v -f pdf -o /phpdocx/docimages/testing.pdf %s 2>&1",
escapeshellarg($source_file));
echo shell_exec($command);
echo "yoyo";
if(shell_exec($command)){
echo "successful";
}else{
echo "failed";
}
?>
This is my edited code and it said sprintf(): Too few argument!
You need to escape the shell arguments, otherwise the shell may interpret its special characters:
$source_file = 'c++ documentation.docx';
$command = sprintf("unoconv/0.7/bin/unoconv -h -v -f pdf -o /phpdocx/docimages/testing.pdf %s 2>&1",
escapeshellarg($source_file));
Provide full (absolute) path to your unoconv/0.7/bin/unoconv executable, as it seems unavailable from your $PATH environment variable:
$command = sprintf("/path/to/unoconv/0.7/bin/unoconv -h -v -f pdf -o /phpdocx/docimages/testing.pdf %s 2>&1",
escapeshellarg($source_file));
Alternatively, include the absolute path to unoconv/0.7/bin/ directory into the $PATH environment variable before calling the shell. Refer to this post, for instance.
Obviously, the $source_file should be in the current directory. Otherwise, the command will fail to access it.
Consider the following code
echo "tar -cvf $outpath -C $path $file<br>";
$arr = array();
$val = exec("tar -cvf $outpath -C $path $file", $arr);
var_dump($arr);
echo outputs tar -cvf ../folder/subfolder1/1234.tar -C ../folder/ subfolder2 and this command really create archive if I call it from command line from htdocs folder. However exec do nothing. var_dump($arr) prints empty array. So here are the questions
How can I make exec create archive?
Why empty array is outputed?
UPD. please note, that I'm running windows.
For those who'll read this question: adding 2>&1 to command helped. And I used shell_exec instead of sheel
$val = shell_exec("tar -cvf $outpath -C $path $file 2>&1");
echo "$val"; // prints output from tar
I have a shell script(i.e mat.sh) in /var/www/ and also have a php script(i.e. n1.php) in the same directory. When i am running the shell script from bash command it is running, but when i m executing the same from the apache2 php server it is not executing.
mat.sh contains..
#!/bin/bash
cat <<EOF | /var/www/matlab -nodesktop -nosplash -nodisplay /> result.out
a=add(2,3);
disp('this is done');
disp(a);
exit
EOF
note:/var/www/matlabis the directory of matlab link
n1.php contains..
<html>
<body>
<?php
if ($_GET['run'])
{
# This code will run if ?run=true is set.
echo "Execution starts here...<br/>\n";
echo exec("whoami");
echo "<br/>\n";
exec ("chmod a+x mat.sh", $output, $return);
if ($return != 0)
{
//An error occured, fallback or whatever
echo "Execution failed<br/>\n";
}
exec ("sh mat.sh");
}
?>
<!-- This link will add ?run=true to your URL, myfilename.php?run=true -->
Click Me!
Please help me.....
Do you get your error message when having done chmod?
Make sure that mat.sh has the same owner as your apache2-server-instance otherwise this line
exec ("chmod a+x mat.sh", $output, $return);
will fail, because only the owner can change permissions.
I have to execute a php script (test.php) in the background. I tried this but it's not working:
<?
$cmd = "php /home/megad404/www/prove/test.php &> /dev/null &";
exec('/bin/bash -c "'.$cmd.'"',$output,$return);
if($return===0)
{
echo 'Successful';
}
else
{
echo 'Unsuccessful';
}
?>
It returns "Successful" but it doesn't execute test.php
test.php:
<?
file_put_contents(date("s"),"");
sleep(5);
file_put_contents(date("s"),"");
sleep(5);
file_put_contents(date("s"),"");
?>
test.php writes a file every 5 second and it works fine, except if I try to execute it in the background with the first script.
Could it be a server issue? Is there another way to run a script in the background?
Use shell_exec and nohup
shell_exec("nohup php /home/megad404/www/prove/test.php > /dev/null & echo $!");
Use shell_exec and give absolute path for php:
$output = shell_exec("nohup /usr/bin/php7.0 -f /home/megad404/www/prove/test.php &> /dev/null &");
Just confirm the absolute path for php in your server. For instance, I'm using php 7.0 and the absolute path is /usr/bin/php7.0
Also, give executable permission to the php file you are running from your code.
chmod +x /home/megad404/www/prove/test.php