Ketan Malhotra
Ketan Malhotra

Reputation: 1250

Giving permissions to webserver without changing folder permissions to 777

I've tried to use chmod function in php to change permissions to 777 temporarily, upload the file and then change it back to 755. But it didn't work, as it doesn't allow me to use the chmod function via php.

if(chmod($path, 0777) ) {
   if(!move_uploaded_file($oldfile, $newfileloc)) {
      return false;
   }
   chmod($path, 0755);
   return true;
}
else
   return false;

I had it working on my previous server with 755 permissions given to the folder.

I'm not sure how permissions work, so please help, thanks!

EDIT: What permissions should my /var/www folder have so that web-server can write files?

EDIT 2: Okay, I had this figured out. I just have to give permissions to www-data:www-data to make sure webserver has all the required permissions. But, the issue I'm getting is that when I have /var/www has chown www-data:www-data, the php functions are working fine but I'm getting permissions denied error when using FileZilla. So right now I have to change permissions to root:www-data everytime I need to transfer something via FileZilla and then back to www-data:www-data to make sure my webserver's working fine. Anyone got a fix for this?

Upvotes: 1

Views: 1131

Answers (1)

Sanny Patel
Sanny Patel

Reputation: 450

you can give 755 permission. But You have to change owner and group for /var/www/ folder. It should have www-data's ownership and group ownership. Check first which user has ownership and group ownership for this folder. run this below command.

ll /var/www/

if it has root access then it would look like this.

drwxr-xr-x. 2 root root 23 Mar 21 17:33 html

change the owner and group owner to www-data user using below command.

chown -R www-data:www.data /var/www

You can keep folder permission 755. -R option is use for giving permission recursively to its child folders and files.

Upvotes: 1

Related Questions