Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

zirby's avatar
Level 4

php shell_exec

Hello, I've got a problem with my php code in Laravel:

 $output=exec(Storage::disk('maj')->get("maj.bat"));
return $output;

my" maj.bat" file:

cd c:\xampp\htdocs\gescrem2
git status -uno

And I get nothing in my $output ????? (no error)

Although I get in my terminal:

On branch master
Your branch is up-to-date with 'origin/master'.
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

        modified:   app/Http/Controllers/HomeController.php

no changes added to commit (use "git add" and/or "git commit -a")

Is somebody can help me, Thanks.

0 likes
5 replies
lostdreamer_nl's avatar

exec only returns the exit status of what it's executing.

Try this way:

exec(Storage::disk('maj')->get("maj.bat"), $output);
// $output is an array of lines now 
dd($output);

zirby's avatar
Level 4

as i said nothing in my output even with dd($output)????

Do mean that if the cmd maj.bat works I did get nothing on return? And If it does not work?

I get the same nothing response !!

lostdreamer_nl's avatar

if you batch file doesnt close with an exit code, then yes, in all cases

$output = exec('some command');

will have no output.

But the exec command does not supply output in the return value, only the EXIT code of the program.

http://php.net/manual/en/function.exec.php output: If the output argument is present, then the specified array will be filled with every line of output from the command. Trailing whitespace, such as \n, is not included in this array. Note that if the array already contains some elements, exec() will append to the end of the array. If you do not want the function to append elements, call unset() on the array before passing it to exec().

// $output should be given as a second parameter
$output = [];
exec(Storage::disk('maj')->get("maj.bat"), $output);
dd($output);
// this should be outputting something like:
/*
Array [
    'On branch master',
    'Your branch is up-to-date with "origin/master".',
    'Changes not staged for commit:',
    '  (use "git add <file>..." to update what will be committed)',
    '  (use "git checkout -- <file>..." to discard changes in working directory)',
    '',
    '        modified:   app/Http/Controllers/HomeController.php',
    '',
    'no changes added to commit (use "git add" and/or "git commit -a")',
]
*/
zirby's avatar
Level 4

Thanks for your answer lostdreamer_nl, but it does not work :-( Still get empty array!

zirby's avatar
zirby
OP
Best Answer
Level 4

I got it :-) not perfect, but it works.

 system("cmd /c C:\xampp\htdocs\gescrem2\storage\maj\maj.bat");

Please or to participate in this conversation.