pythonphpbatch-fileexec

PHP exec() fails when trying to run .bat file


I have a server running from XAMPP on my pc and when I try to run exec('generate_image.bat') it returns false, so it fails. However when I run the .bat myself it runs perfectly. The exec method also runs perfectly when I run a second, 'test.bat' file, located in the same directory. The script is supposed to start a python program, that generates an image with Stable Diffusion.

Here are my PHP and .bat codes:

PHP:

<?php
    $command = 'generate_image.bat';
    if(!exec($command, $output)) {
        echo "Exec failed";
    } else {
        echo "Exec finished\n<br>\n";
        echo implode("\n", $output);
    }
?>

generate_image.bat

@echo off
g:
cd "G:\User\VS Code\Python\Stable Diffusion"
python main.py

test.bat

@echo off
echo Test
echo Test2

I cannot figure out what's going on.

Tried running the file both with system() and with exec(), using both relative and full paths, and outside of code (this one worked).


Solution

  • I figured out the problem after looking at the Apache's error.log:

    Traceback (most recent call last):
      File "G:\User\VS Code\Python\Stable Diffusion\test.py", line 1, in <module>
        import requests
    ModuleNotFoundError: No module named 'requests'
    

    My modules were only installed to my user, I fixed this by uninstalling them, then opening cmd in administrator mode and running pip install.

    Works like a charm!