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?

7
  • 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? Commented Sep 19, 2016 at 18:23
  • How can i check that? @albert Commented Sep 19, 2016 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 Commented Sep 19, 2016 at 18:27
  • run you php script by root permission. sudo script.php Commented Sep 19, 2016 at 18:31
  • 1
    Possible duplicate of Getting PHP to run a Python script Commented Sep 19, 2016 at 18:35

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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

Refernce: PHP Manual

2 Comments

This is more of a way to debug, so it should be a comment, if I'm understanding correctly.
I don't need to capture output, i want python script to execute and create files. In fact using shell_exec() has no effect

Your Answer

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.