0

I tried many solutions but nothing it works :

echo '<pre>';
shell_exec("python /home/folder/python/mapfile_compress.py");
shell_exec("sudo -u wwwexec python escapeshellcommand(/home/folder/python/mapfile_compress.py) $uid");
shell_exec("sudo chmod +x /home/folder/python/mapfile_compress.py");
system("bash /home/folder/python/mapfile_compress.py");
passthru("bash /home/folder/python/mapfile_compress.py");
passthru("/home/folder/python/mapfile_compress.py");
exec("bash /home/folder/python/mapfile_compress.py");
echo '</pre>';

I launched indivdually them but in all cases, Firebug returned : '<pre>'

So I tried this code founded on Stack Overflow :

$command = escapeshellcmd('chmod +x /home/folder/python/mapfile_compress_test.py'); echo $command; $output = shell_exec($command); echo $output;

But firebug returned nothing. My python file begin with #!/usr/bin/env python and if I launch it on server that works !

Do you knwo how can I launch my python file from PHP file ?

asked Jun 29, 2017 at 7:41

3 Answers 3

1

chmod will return 0 on success and> 0 on error.

Make sure that the file is able to run by just executing it as the web user. When +x is properly set, you can execute it by just calling $ /path/to/your/file.py, the shebang in the first line in your script #!/usr/bin/env python should define the correct python based on your env.

You can test this by running:

$ /usr/bin/env python /path/to/your/file.py

So check your file permissions to check if the file is executable by the user that runs the php script.

Just to test, you can just print a few lines in your python file

#!/usr/bin/env python
print "test line 1"
print "test line 2"

Then if you have verified permissions and the correct use of python, you can do this in your php.

$command = escapeshellcmd('/path/to/your/file.py');
$output = shell_exec($command); // get all output or use passthrough, exec will only return the last line.
echo "<pre>{$output}</pre>;
answered Jun 29, 2017 at 7:58
Sign up to request clarification or add additional context in comments.

3 Comments

Ok, so I try to use chmod from my php file with this code : chmod ("/home/folder/python/test.py"); but it's the same thing firebug return nothing
Do you have to use chmod from php? The file you are trying to execute should be available and executable on your server before executing it from php.
Yes I launched it from PHP file. I can only launch it from PHP file but on my PC, my python file : test.py works
0

At first, Do you have enabled shell_exec/system/passthru commands in php.ini?

shell_exec("python /home/folder/python/mapfile_compress.py");

I think, it could be problem with your $PATH. Try something like: (use full path to python)

shell_exec("/usr/bin/python /home/folder/python/mapfile_compress.py");
shell_exec("/usr/local/bin/python /home/folder/python/mapfile_compress.py");
answered Jun 29, 2017 at 7:47

Comments

0

In my case that's work if I write this code :

$command = escapeshellcmd('python /path/to/your/file.py');
exec($command);
answered Jul 3, 2017 at 9:35

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.