Accessing (Read,write) file from PHP


Have thrown whole day trying to find a solution or any workarounds for the problem. I am trying to read/write files to /var/www/emoncms/Modules/mymodule/ path (There are some configuration files that the PHP functionality depends on) and while using WinSCP to access and edit the files the IP/emoncms/mymodule functions work well without giving any errors (upon opening is given permission to write via sudo su && rpi-rw call) but when rebooting the device and just accessing the PHP page via browser there are displayed errors as the PHP can not open nor write files. Also calling the same sudo su && rpi-rw call via PHP seems to not be working. As I have figured out theres something to do with the user group permissions that the PHP apache has to get permission/ownership for the files to be able to change. If anybody can help the help is much appreciated at this point :slight_smile: .

Also if there is any location that the PHP is already allowed to write it would be nice to know as I could also use that location for the configuration files.

Thank you in advance!!!

You could point your PHP at the data partition, that is always RW and emoncms can write to that partition no problem.

1 Like

Thanks for the suggestion! I will be trying it out soon :smiley: .

That solution would work yes but theres one thing I forgot to mention before is that wpa_supplicant.conf in etc folder gets also data written into it so that also needs another solution. I am probably going to do some backtracking on how the original WIFI settings saving works and sort that out like that. Thanks for the answer!!!

Are we talking about a emonSD image?

The wpa_supplicant.conf file should already be writable as it is also (re)located on the data partition and has a symlink in the original location so even when /etc/wpa_supplicant is RO the wpa-supplicant.conf remains RW.

[email protected](ro):~$ ls -la /etc/wpa_supplicant/wpa_supplicant.conf
lrwxrwxrwx 1 root root 33 Jan 26  2016 /etc/wpa_supplicant/wpa_supplicant.conf -> /home/pi/data/wpa_supplicant.conf

Does your application “edit” or “overwrite” the file? It may be that instead of editing the file via the symlink, it is overwriting the symlink, placing it in the RO folder rather than retaining its place in the data partition.

I imagine this would also be the result if using the official Rasbian solution of placing a wpa_supplicant.conf file in the /boot partition before booting, as the OS would normally move the file from /boot/wpa_supplicant.conf to /etc/wpa_supplicant/wpa_supplicant.conf However the read-only OS of the emonSD would probably foil any attempt to move the file, although I have to say I haven’t tried it.

There was supposed to be editing or overwriting. The main idea was that the information in wpa_supplicant should have been updated with new preferences to use hidden WIFI network. I fixed the problem and in case someone searches for the solution in the future, then he/she can find it here. Also, thanks Paul. I wouldnt have found a way without discussing the matter with you.

Anyway, the solution I found was that the EmonCMS uses account www-data to access files, so no root access. As the www-data does not have any priviledges to other files but only stated methods per actions are allowed, I had to change etc/sudoers file. Theres line with www-data had allowed accesses for console commands. I got that name running BASH script with echo “whoami” in PHP. Theres also sudoers files per environment stated in the same file so you can also state only EmonCMS access per user. So anyway editing the access for files allowed me to overwrite it.

Also, if I recall righ,t the native solution already in SD image for EmonCMS allows www-data to write newwifi.conf file in temporary files folder and with cp function it allows to copy the file in etc/wpa_supplicant.conf overwriting the file. Hopefully this helps someone in the future!