2

I have php code on Codeanywhere where I'm simply trying to create a file on the server but yet gets the following error :

"Warning: fopen(file.txt): failed to open stream: Permission denied in /home/cabox/workspace/index.php on line 312"

I use fopen ($file, 'w'). with 'r' as parameter, it works.

I asked CodeAnywhere if there were any restictions coming from them to prevent users from having code able to create files directly on the server but was answered that there were no restrictions and only had to look up how to grant permissions.

First Used chmod for both files and directories

chmod 755 index.php (afterwards 777 for experimental purposes)

Yet I'm still confronted with the error message shown above. So I dug into the permissions at each level of the server and displayed them below but I do not see anything strange (according to my meager knowledge)

/

[cabox@box-codeanywhere /]$ ls -ld
drwxr-xr-x 21 root root 4096 Apr 19 03:09

/home/

[cabox@box-codeanywhere home]$ ls -ld
drwxr-xr-x 3 root root 4096 Jun 16 2014

/home/cabox/

[cabox@box-codeanywhere ~]$ ls -ld
drwxrwxrwx 7 cabox cabox 4096 Apr 14 11:48

/home/cabox/workspace/

[cabox@box-codeanywhere workspace]$ ls -ld
drwxrwxrwx 4 cabox cabox 4096 Apr 19 04:27

I am not able to change the permissions for / as the user name is root I imagine that is because my user name is cabox

[cabox@box-codeanywhere workspace]$ id
uid=500(cabox) gid=500(cabox) groups=500(cabox)

index.php trying to create file.txt is located in /home/cabox/workspace/

-rwxrwxrwx 1 cabox cabox 18608 Apr 19 05:12 index.php

what am i doing wrong?

Thanks in advance!

1

After much experimentation, I discovered that I had set up my virtual machine development environment as Html5 and not php. Php files were working correctly as the server was apache but for some reason that still eludes me, the management of permission is different.

After switching the environment to PHP, commands that didnt work previously did and was finally able to figure out the problem.

Here is the "solution" to be taken with a grain of salt as my proficiency in the matter is rather limited

Giving your own user (cabox) the 775 right does nothing, as only the 'www-data' user has the right to create, modify, delete files on the server, when the website is being executed.

sudo chown -R www-data:www-data /home/cabox/workspace chmod 775 /home/cabox/workspace

this did the trick.

Now, on codeanywhere, if I change the user name to 'www-data', I lose my ability to work on the server, create, modify, delete files. I thought the solution was to set the rights as user (cabox) and group (www-data) and give them the same rights, but so far that has not worked so still need to figure out that bit

Hope that helps people who will encounter the same issue in the future. It took me a week, a rather frustrating one :)

0

You might want to change the owner of the file or directory

chown username -R yourfolder

. I had the same problem and this fixed my issue..

  • (1) The ls -ld outputs in the question indicate that the user (cabox) already owns everything he's supposed to own, so there's nothing to be gained by chowning things.  (2) The OP indicates that he isn't root ("I am not able to change the permissions for / ..."), so he wouldn't be able to chown anything anyway. – G-Man Apr 19 '15 at 23:36
0

Maybe this could work, i was having the same issue and after 3hs of googles i tried this:

Folder Permission

The last and final thing you need to do to complete your installation is to set the folder permissions. To do this, open up your command line and type:

sudo find . -type f -exec chmod 664 {} + 
sudo find . -type d -exec chmod 775 {} + 
sudo chown -R vm_username:www-data ~/workspace

at least for me, solve the permission´s issue i was having.

  • Could you explain what these commands actually do? – Burgi Aug 25 '16 at 15:19
  • im not a expert on linux sorry, BUT from my knowledge ` sudo find . -type f -exec chmod 664 {} +` search any file "type f" and apply the chmod 664 and the "type d" any folder an apply a chmod 775 and the the chown -R set the permission to www-data to the folder workspace – jparral Aug 25 '16 at 16:37
  • This was a suggestion to edit your answer to improve it. Please see How to Answer and take our tour. – Burgi Aug 25 '16 at 17:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.