0

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)

2
  • Can you pass the code example ? Commented Jan 22, 2022 at 17:14
  • @Gytree Yes, I have edited question Commented Jan 22, 2022 at 17:27

2 Answers 2

1

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
Sign up to request clarification or add additional context in comments.

6 Comments

I have refactored code and i got output: array(2) { [0]=> string(62) "'spotdl' is not recognized as an internal or external command," [1]=> string(31) "operable program or batch file." } And as i said - spotdl is working fine on cmd run by hand.
How you install the spotdl?
Followed official git guide given here
maybe is an issue with the laragon and the path.
Okay, so i have found a solution: 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.
|
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.