4

Is there any special permission or setting in order to execute a python script that create a new file or image? This is my code:

<?
function py(){
    exec( 'python my_script.py');
    echo "ok";
}

py();
?>

And this is my python script (an example):

#!/usr/bin/python
import sys

f = open('out.txt', 'w')
print >> f, 'thats all'
f.close()

Running php i get "ok" message but no file is created. What am i doing wrong?

slash89mf
  • 1,057
  • 2
  • 13
  • 35
  • PHP should be executed by user `www-user`. So the Python script would be stared from the same user. Does `www-user` has the desired permissions to execute the Python script and access file-system for file-creation? – albert Sep 19 '16 at 18:23
  • How can i check that? @albert – slash89mf Sep 19 '16 at 18:24
  • I have never done this since I am not a PHP user so everything I am talking about is just mind-based. First you should find out which user is the owner of the folder and the script you want to access. If this is not `www-user` you could either change the user to `www-user` or pass the desired user name to the `exec` command as shown [here](http://stackoverflow.com/questions/6913403/running-command-line-application-from-php-as-specific-user) – albert Sep 19 '16 at 18:27
  • run you php script by root permission. sudo script.php – Mehrdad Dadvand Sep 19 '16 at 18:31
  • @MehrdadDadvand i can't, php file is executed via ajax when a button is clicked. – slash89mf Sep 19 '16 at 18:32
  • 1
    Possible duplicate of [Getting PHP to run a Python script](http://stackoverflow.com/questions/2219982/getting-php-to-run-a-python-script) – cmorrissey Sep 19 '16 at 18:35
  • @albert not working – slash89mf Sep 19 '16 at 20:22

2 Answers2

1

Setting the right permissions to file and folders did the trick.

enter image description here

On Mac OS: Alt + Click on "+", then search for _www and add World Wide Web Server to users giving read and write permission. Same for the root folder.

slash89mf
  • 1,057
  • 2
  • 13
  • 35
0

You need to call shell_exec() not exec() if you want to be able to capture the output:

Refernce: PHP Manual

NSNoob
  • 5,548
  • 6
  • 41
  • 54
Mike Mannakee
  • 361
  • 1
  • 5
  • This is more of a way to debug, so it should be a comment, if I'm understanding correctly. – Goose Sep 19 '16 at 20:21
  • I don't need to capture output, i want python script to execute and create files. In fact using shell_exec() has no effect – slash89mf Sep 19 '16 at 20:22