0

I have a open() command in my Python script which opens the file data.json in my Apache serve running on Raspberry Pi in w mode. This script in turn is run by PHP using the shell_exec command. When the script is run alone, the Python code works. However, it does not function when run by PHP. Does anyone have any idea why this is happening, or is more information needed? Thank you in advance for your help!

Tiskolin
  • 127
  • 16
  • Please note that I am currently not at my workstation, so I cannot provide ant snippets of code if they are needed. – Tiskolin Nov 23 '17 at 21:48
  • 1
    I am assuming you have some kind of Linux distro running on your Pi. Does your Python file include a shebang line (https://stackoverflow.com/questions/2429511/why-do-people-write-usr-bin-env-python-on-the-first-line-of-a-python-script/2429517)? I think that was a problem that I had some time ago, when a Python file wouldn't run from PHP on some kind of Linux server. – Niellles Nov 23 '17 at 22:04
  • Thanks for the advice... as I said previously, I’m currently not at my workstation, so I can’t try it out. Thing is, PHP could run it fine *until* I started using the open() function. - and yes, I am running Rasbian and no, I don’t have that line of code, so it might be the problem. Thank you! – Tiskolin Nov 23 '17 at 22:09
  • 1
    Sounds like a privilege issue, although I am not experienced enough with calling Python from php to know whether that could be an issue here. – Niellles Nov 23 '17 at 22:24
  • I’ve chmod 777 * everything... so I don’t think that should be a problem. Well, unless data.json is not made read/write yet. I’ll check when I get back home. – Tiskolin Nov 23 '17 at 23:10

2 Answers2

1

Did you use the relative or absolute path?

Try to use the absolute path and check what's your working directory. I think you are starting the python script not in the directory you think you are. :)

Tiskolin
  • 127
  • 16
Demigod
  • 96
  • 11
  • I am using the relative path (just ‘data.json’ not ‘path/to/data.json’). P.S. I like your user name. ;) – Tiskolin Nov 23 '17 at 23:12
0

Thank you to user2693053 for solving this problem:

Sounds like a privilege issue, although I am not experienced enough with calling Python from php to know whether that could be an issue here.

and yes, it is a privilege issue! I fixed the problem by doing the sudo chmod 777 * command in the /var/www/html directory.

Tiskolin
  • 127
  • 16
  • 1
    It would be much more secure, if you create a new user group, which owns the files. In this group, is the user you are running the scripts with, and the httpd user. Rights of 777 are really unsecure. – Demigod Nov 30 '17 at 22:14
  • Sorry about not responding sooner, I got a question ban for a couple of downvotes on my other posts, so I haven't been on Stack Overflow as much... sorry. – Tiskolin Dec 01 '17 at 01:47