skip to Main Content

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


  1. Chosen as BEST ANSWER

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

    Traceback (most recent call last):
      File "G:UserVS CodePythonStable Diffusiontest.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!


  2. 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 from

    @echo off
    g:
    cd "G:UserVS CodePythonStable Diffusion"
    python main.py
    

    to

    @echo off
    c:python3.9python.exe "G:UserVS CodePythonStable Diffusionmain.py"
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search