I have a PHP script that calls a Python script (with arguments):
<?php
$UPLOAD_DIR = 'tmp';
$AUDIOSPLIT = "../audiotranscri/scripts/audiospliter.py";
// This also fails using a symlink in the same dir
//$AUDIOSPLIT = "spliter";
$media_path = "tmp/media.mp3";
$subtitles_path = "tmp/mediasub.ass";
$cmd = "python3 ".$AUDIOSPLIT." ".$media_path." ".$subtitles_path." ".$UPLOAD_DIR;
echo($cmd);
system($cmd,$returnv);
echo($returnv);
?>
It works when i run this from a bash terminal
$ php7.2 dummy.php
But fails when call it from a website (Apache2 with PHP). Why ?
N.B: I am sure Apache and PHP are running OK since phpinfo() works perfectly. Versions are PHP 7.2.15, ubuntu 18.04.1, Apache 2.4.29.
Consider trying one of these:
use full path for your program as PATH might not be set when using system() - change "python3 " to "/usr/bin/python3 ", or wherever your python3 is.
Using another function, exec(), shell_exec(), passthrough()
Check if your web server allows you to run given functions when running through http, check in your php.ini, or check with php script accessed through http
<?php
$function_name = "system";
if ( function_exists($function_name) ) {
echo "$function_name is enabled";
}
else {
echo "$function_name is not enabled";
}
Related
So i have a project where I send spotify track URL to server and store it. I use laragon with PHP 7.4 installed, windows 11. I use that downloader. It works via cmd command line. When I try to use it by hand it works fine, but when i send it by exec() it doesn't work. I can't get any return message neither. I have tried using exec(), system() and shell_exec() functions. The problem might be with with system variables, as these might not be visible for PHP. I have also tried turning on/off server and it didn't work. I have also tried to put putenv('PATH=' . $_SERVER['PATH']) at the beginning of file. I tried to check laragon path variables itself - I couldn't see these i have added. Any other default windows commands works as should be. Any ideas on how to fix it?
Here is my PHP code:
function createFile($url, $token){
function execCommand($dir, $url){
$command1 = "cd $dir";
$command2 = "spotdl $url";
if(!shell_exec($command1. "&& ". $command2)) return false;
return true;
}
$path = "C:\Laragon/laragon/www/temp/";
$dir = $path.$token.'/';
if(!mkdir($dir, 0777)) throwError("Server error");
if(!execCommand($dir, $url)) return false;
return true;
}
I know i'm not returning any output from console, but that is post updates. Second command is definitely beeing called, i have tested it on some other commands (like mkdir)
In many os the errors output (stderr) of the commands isn't the same that the normal output (stdout), you need to redirect the errors to the stdout. so the command must be:
$ command $arg1 $arg2 ... 2>&1
This cause that the errors messages will be sent to the stdout, Is util remember this when you are working with system calls.
Now in your code i prefer to use the exec function in php
<?php
$result = 0;
$output = [];
$command = "your command with the 2>&1";
exec($command, $output, $result);
if ($result != 0) {
// here an error ocurred and you can see the error on the ouput array
exit();
}
// here you know that the command was executed successfully
After re-installing laragon it worked. I Have checked before uninstalling path variables and i haven't seen mine that should be in.
I couldn't modify it either so I just reinstalled it.
What I want to achive:
I want to update my git repository (which holds my laravel app) on my shared host whenever i push something into my repository. To do so, I have a deploy.sh script on my shared host which pulls my repository and deletes all caches from my laravel app.
In order to run this bash script automatically I also got a php script which is triggerd by a Github webhook whenever i push something into my repository.
My problem:
I try to run my bash script on my shared host with my php script but that doesn't work.
What i tried:
My bash script by itself runs perfectly fine when I execute it manually on my shared host via ssh. But it won't get executed by my php file.
PHP
<?php
function debug_to_console($data) {
$output = $data;
if (is_array($output))
$output = implode(',', $output);
echo "<script>console.log('Debug Objects: " . $output . "' );</script>";
}
$access_token = 'maxi9000';
$client_token = $_GET['token'];
if ($client_token !== $access_token)
{
echo 'error 403';
}
else{
$old = getcwd();
echo '</br>'.$old;
$file = $old.'/mhraschan.com/deploy.sh';
echo '</br>'.$file;
if(is_file($file)){
echo '</br>Is file';
if(!is_executable($file)) {
chmod($file, 0755);
echo '</br>file not executable - run chmod';
if(is_executable($file)) echo "File is now executable";
}
//Try to run bash file
$return = shell_exec('bash '. $file);
echo '</br>Shell return: '.$return;
debug_to_console('Shell return'.$return);
}
}
?>
Output of my browser when running the php script manually:
/data/web/e115665/html/test
/data/web/e115665/html/test/mhraschan.com/deploy.sh
Is file
file not executable - run chmod
Shell return:
So my file is not executable and runs chmod but after that it seems that it still is not executable because it doesn't run that line again: if(is_executable($file)) echo "File is now executable";
Also i dont get any output from my shell_exec command.
Any solution?
This is almost certainly an issue with permissions - remember, your web server is not running as root; if you're using Apache, you're probably running as www-data - other web servers will use other users. Thus, your attempt to chmod the file is failing, since you don't have permission to change the permissions on that file. And if the permissions for the file do not include "executable" for the web server's user, the script will not run.
Check the return value from the chmod function - I think you'll find it's false (meaning that it failed, per the documentation).
I created a php file as shown below.
<?php
$ar = 4600;
$az = "redshift -O ".$ar;
echo "Command executed: ";
echo $az;
system($az,$status); // It is not working on browser but working on terminal.
system("ls",$asd); // It is working on both browser and terminal.
if($status == NULL) {
echo "<h3>Operation not successful</h3>";
}
else
echo "<h3>Temperature Succesfully set to ".$ar."</h3>";
?>
Now, the matter is that
when i am running this file on terminal using command
php file.php
the 'ls' command is getting executed and 'redshift -O 4600' also getting executed.
But when i am executing this file on browser using url.
127.0.0.1/file.php
Only 'ls' command is getting executed. 'redshift -O 4600' is not getting executed. Is there some way through which i can execute such programs.
I have also tried other functions like exec etc.
It would not work as you are setting up your server at /var/www/html location which is used by public to access your computer through localhost. And since in localhost there is no such thing as redshift so it would run nothing and the operation would be executed and you will see no after effects. So, to run the system's command on localhost, you must run the server on any folder other than /var/www/html. So, to run the server on any other location, just go to that location and run
php -S localhost:8080.
I use the testing framework DalekJS to test the user interface. To execute my testing script mytest.js I type into the shell:
cd /Applications/XAMPP/xamppfiles/htdocs/tests
dalek mytest.js
and it works fine. Now I would like to execute the same script with PHP. My code:
<?php
chdir('/Applications/XAMPP/xamppfiles/htdocs/tests');
$command = 'dalek mytest.js';
exec($command, $return, $return_var);
var_dump($return);
var_dump($return_var);
Running my PHP-script in browser, it prints:
array(0) { } int(127)
The DalekJS script generates a screenshot when executing in shell but running with PHP it does not happen anything. I have also tried shell_exec(), system() and passthru() without success.
Do you have any idea why the PHP script does not work?
Calling dalek from PHP works fine for me. Is it possible your PHP process is running with a different environment (containing PATH, amongst other things) that your user? Maybe the apache user or some such?
<?php
// change current working directory of the current PHP process,
// this will subsequently change the initial CWD of exec()
$whereMyTestsAre = __DIR__;
chdir($whereMyTestsAre);
// locate dalek
$dalek = exec('which dalek');
// abort if there is no dalek,
// you may want to check PATH or supply the full path yourself
if (!$dalek) {
echo "could not find dalek executable, please check your path";
$PATH = exec('echo $PATH');
echo '$PATH is ', $PATH, "\n";
exit(1);
}
// relative to $whereMyTestsAre
// exec() blocks until the child process (dalek) exits
exec('dalek mytest.js');
I'm running PHP 5.4.9 on Windows server
I've tried running all script commands in PHP (exec, shell_exec, system, proc_open, passthru). All seem to return empty or null.
I've added phantomjs as a PATH variable.
And running phantomjs --version in command prompt, and it returns 1.8.2
Although when I try to run
$return = exec("phantomjs --version")
or
$return = shell_exec("phantomjs --version", $output)
$return is always null and $output is empty.
I made sure IUSR and IIS_IUSRS users have permission to run phantomjs.exe
Safe mode is disabled in php.ini
Also, I tried running exec('ls') && exec('ipconfig /all'), and those output the data I'm expecting.
I'm not sure what else to try.
I was facing the same problem..
The thing is phantomjs requires complete path for all
Here is the solution I came up with:
$getout = exec('D:\\xampp\\htdocs\\phantomjsdir\\phantomjs.exe D:\\xampp\\htdocs\\rasterisejsdir\\rasterize.js http://localhost/pagetobecaptured/test D:\\xampp\\htdocs\\outputfiledir\\test2.jpg "1800px*840px"',$o,$e);
You are pretty close to a solution. It's basically:
$stdout = shell_exec('time /T');
echo $stdout;
You need to make sure, that the Phantom binary is either on path or called with full-path.
For a full example executing PhantomJS, see the driver file of "jakoch/PHPUnit-headless".