0

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).

Compo
  • 36,585
  • 5
  • 27
  • 39
Kaboka22
  • 1
  • 2
  • Did you check this out - https://stackoverflow.com/questions/835941/how-do-you-run-a-bat-file-from-php – Mohammed Jhosawa Aug 28 '23 at 15:31
  • 2
    What is the `.bat` for? Can't you run your `python.exe main.py` directly via `exec()`? – brombeer Aug 28 '23 at 15:56
  • Unfortunately not, since I noticed that messes with relative paths (it wouldn't find temp.png for example, which was in the directory of the python code). – Kaboka22 Aug 28 '23 at 18:14
  • Does this answer your question? [How do you run a .bat file from PHP?](https://stackoverflow.com/questions/835941/how-do-you-run-a-bat-file-from-php) – Ken Lee Aug 29 '23 at 00:47
  • No, I tried all of the answers from here. – Kaboka22 Aug 29 '23 at 07:41

2 Answers2

0

When you run exec in PHP, you should not rely on the environment variables (including path) to execute the command(s). Hence, you should provide full-paths.

So, assuming that the python.exe is in location c:\python3.9\python.exe, then please change generate_image.bat from

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

to

@echo off
c:\python3.9\python.exe "G:\User\VS Code\Python\Stable Diffusion\main.py"
Ken Lee
  • 6,985
  • 3
  • 10
  • 29
  • If there is a need to change the working directory, which you haven't catered for in your answer above, use ```@PushD "G:\User\VS Code\Python\Stable Diffusion" 2>NUL || Exit /B``` as the first line instead, then use ```@"C:\python3.9\python.exe" "main.py"``` as the second line. Or do it in a single line, ```@PushD "G:\User\VS Code\Python\Stable Diffusion" 2>NUL && "C:\python3.9\python.exe" "main.py"```. – Compo Aug 28 '23 at 16:12
  • I tried this, however sadly the problem still remains :/ – Kaboka22 Aug 28 '23 at 23:24
0

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!

Kaboka22
  • 1
  • 2