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.
Related
I have a problem when i try to run this exec statement. The exec wont start the php shell file as i want it too.
I've tried using shell_exec but that wont work either. And when i try to echo out the returning array and var i get the var to "1" and an empty array.
Here is my code I'm trying to execute:
<?php
$user = $_GET['u'];
session_start();
if ($_SESSION['user'] === $user) {
$sID = $_GET['si'];
exec('php C:\\xampp\\htdocs\\r\\pages\\create-csgo-server3.php');
echo 'Server is being created.<br>';
} else {
echo 'Permission denied.';
}
?>
It says "Server is being created." but it wont run the exec correctly.
php C:\xampp\htdocs\r\pages\create-csgo-server3.php
I tried putting this over into the cmd and it worked correctly.
Thank you for taking your time!
I'm sure there is a duplicate, but while I'm searching; the webserver user (user that runs Apache or IIS, etc.) doesn't have the path to php.exe in its path environment variable, but you must as it works for your user in a terminal. Always use full paths:
exec('C:/path/to/php C:/xampp/htdocs/r/pages/create-csgo-server3.php');
On my Linux server I use shell_exec() to run scripts asynchronously. This function is not available on Windows, which I use for development, and so I am trying to use popen() instead.
I am running Windows 10 x64, WampServer 2.5 x32 (PHP 5.5.12).
I have the following code in a PHP file:
$cmd = '"C:\wamp\bin\php\php5.5.12\php.exe" "C:\wamp\www\path\to\file.php" "arg1" "arg2"';
echo 'Running on DEV MACHINE: ' . $cmd . PHP_EOL;
$handle = popen("start /B " . $cmd, "r");
if ($handle === FALSE) {
die("Unable to execute $cmd");
}
pclose($handle);
The script executes without any errors, but the PHP files are never run. I know they aren't being run because I put an SQL query at the very top to update a field in a database to indicate they have been run.
As you can see in the above code, I print out the command being passed to popen(). If I paste that command into command prompt and execute it, the scripts run fine. This proves the issue lies with popen().
Am I using popen() incorrectly somehow? How can I debug what the problem is? Normally I would debug using XDebug, but there's no way to use that in this situation.
I was never able to get my code working, but I found an alternative method from another answer on here and based on that changed my code to use the following and it works:
$WshShell = new \COM("WScript.Shell");
$oExec = $WshShell->Run($cmd, 0, false);
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".
I have the following PHP code:
<?php
$video = "C:\Users\Administrator\myVideo\processing\video.mp4";
$cmd = 'ffmpeg -i "' . $video .'" 2>&1 | wtee buffer.txt'; // wtee is a Windows version of tee
exec($cmd);
echo($cmd);
?>
If I run aa.php, which contains the above code, it won't work.
But if I run the $cmd that is being echoed out on the command prompt directly, the file buffer.txt is created and works.
I want to get the output of this:
$cmd = 'ffmpeg -i "' . $video .'"'
Into a variable like, e.g. $output.
This code, so far, prints blank:
exec($cmd,$output,$result);
print_r($output);
Like you mentioned, that your code works directly from command line but not from PHP CLI, is most likely because of log file permissions, which means, you must create a log file with correct permissions first, and then write your output there!
For example, from command line (not PHP) make sure that you delete your log file buffer.txt and run your PHP code again. Most likely it will not work as your file doesn't exist. Remember, when you run that command from command line directly, your log file, which is buffer.txt is created with special permissions (not only read/write, but also group and owner (Linux)). When PHP creates a file, by default, it uses nobody nobody for owner/group and you already have a log file (as you ran that same command from command line), which is created by other user (I suppose administrator), as a result you can be sure that PHP will not be able to use it, just because of the wrong file permissions. Try to see what is on your current log file owner/group by executing ls -ls in Linux or DIR /Q in Windows to get the idea.
Besides everything, make sure that you're not running PHP in safe mode, as a result exec might not work as you expect it, because it's disabled if PHP is in safe mode.
shell_exec() (functional equivalent of backticks)
This function is disabled when PHP is running in safe mode.
exec()
You can only execute executables within the safe_mode_exec_dir. For practical reasons it's currently not allowed to have components in the path to the executable. escapeshellcmd() is executed on the argument of this function.
You can check your server's PHP settings with the phpinfo() function.
Your code should rather look like this:
exec("$cmd 2>&1", $output);
log_conversion($config['logs_path']. '/' .$video_id. '_log.txt', $cmd);
log_conversion($config['logs_path']. '/' .$video_id. '_log.txt', implode("\n", $output));
What does that mean? If you say 2>&1 then you are redirecting stderr to wherever stdout is currently redirected to. If stdout is going to the console then stderr is, too. If stdout is going to a file then stderr is as well.
Please read more about command redirections in articles Using command redirection operators for Windows or All about redirection for Linux.
Your log conversion function, could be something like this:
function log_conversion($file_path, $text)
{
$file_dir = dirname($file_path);
if( !file_exists($file_dir) || !is_dir($file_dir) || !is_writable($file_dir) )
return false;
$write_mode = 'w';
if( file_exists($file_path) && is_file($file_path) && is_writable($file_path) )
$write_mode = 'a';
$handle = fopen($file_path, $write_mode);
if( !$handle )
return false;
if( fwrite($handle, $text. "\n") == FALSE )
return false;
#fclose($handle);
}
It's very important to make sure that the log file you create is writable and has correct permissions!
I personally delete the log file in case it exists, to make sure that I have recent and reasonable log file.
I bet it will work either like this or with small tweaking!
If it doesn't work for you, please let me know and I will elaborate!
exec requires an array for the output variable.
So try:
$output = array();
exec($cmd, $output, $result);
That will give you an array where each element is a line returned by your command.