Folder Permission Issue


When I manually copy files from one folder to my www folder, i cannot read those files using php because those copied files do not have read permission (i am on ubuntu linux) so what I have to do is run sudo chmod -R 777 /var/www/html/ followed by my password.

I ahve to run this command manually, but I want to automate this in my script, The chmod() function or exec(‘sudo chmod -R 777 /var/www/html/’); does not work as they dont change the desired permission.

Please help me.


Not sure PHP is the best way to approach this.

What is the output of running:

ls -l /var/www

For php you can try

$file = “path\file”;

chmod( $file, 0777 );

Chmod expects the mode as an octal not decimal.
I agree php isn’t the best for these. If your using ftp you can set the mode they will be uploaded as.

You do not need to run chmod as root if the file belongs to your user. So leave out sudo. Also, instead of running chmod after copying files you could set umask to an appropriate value.

I get this error when running the chmod command

Warning: chmod(): Operation not permitted in /var/www/html/

@phantom007, sounds like you have a much larger problem than what you are telling us about.

First off, NEVER change the files to 0777, that gives everyone on the server read, write, and execute access to those files. That is dangerous.

Second, sudo in a PHP command will never work. As you don’t have an easy way of providing the authenticating password to sudo. So anything that depends on sudo will be a manual process – not what you want.

So let’s get back to the beginning here with a few questions.

  1. What web server are you using? Apache? nginx? lighttpd?
  2. Is this a development server? Or a live production site server?
  3. Any reason you are using the /var/www/ folder? Versus creating a virtual directory in a different location?

I wrote a WordPress plugin a long time ago (for error logging) that had this that worked.

		chmod($main_dir, 0705);
		exec("chmod 604 " . $main_dir . "/*.log");
			exec("chmod 600 " . $main_dir . '/*.log');
			chmod($main_dir, 0700);
1 Like

Check your php.ini it’s probably being blocked. Ask your host if they can unblock it or if it’s yours change the setting to allow it.

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.