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 finishedn<br>n";
echo implode("n", $output);
}
?>
generate_image.bat
@echo off
g:
cd "G:UserVS CodePythonStable 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).
2
Answers
I figured out the problem after looking at the Apache's error.log:
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!
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.9python.exe
, then please change generate_image.bat fromto