OKay - I'm running a PHP script which needs to use the system() command for various operations (copy, mkdir, tar, rm etc).

It's not working in all circumstances:

All folders in my public_html folder are created by me with a CHMOD permission of 755 (user = rwx / group = rx / all = rx) normal users could therefore not write to the folder. This means not making directories, not deleting files etc …

One folder that I tested had permissions 777 (user = rwx / group = rwx / all = rwx) so everyone could write to the folder.

My script(s) only work if the folder has permissions 777.

But this is not good for security as it means any user of your system can edit the files.

Unfortunately the PHP scripts seem to be run as a web user and not as me so they cannot modify my files with 755 permissions. Hence why it works with 777.

Is there anyway that I can instruct PHP to run as me so that I can change my permissions back to 755?

I’m assuming this is a common issue - how do most people get around it?

Any ideas?

Cheers

    6 days later

    Doesn't work šŸ™

    Let me explain in more detail .....

    I can use system() commands, but to do this I need to be able to have 777 permissions on a folder as Apache is run as a completely seperate user. This is obviously bad as people will be able to modify the contents of the folder.

    In all (but one) cases I can use PHP commands like mrdir() and unlink() - that's fine, BUT they do not have permission to run either and they require the permissions to be 777, so I am no better off.

    So I tried using chmod() before (and after) each command, that does not have permission to run.

    So if I can't change permission in PHP I can't see a way around it. I don't want to have to set folders to 777!

    My ISP (www.dataflame.co.uk) seems helpful, but in this instance can't do much.

    Can anyone please think of a way around this, or something that I can email dataflame's support team and ask them to change.

    This MUST be a common problem. Would there be any reason chmod() isn't working?

    The error message given is:

    Warning: chmod(): Operation not permitted in /home/username/public_html/test.php

    One further thing that may give an indication of their setup. If I set folder permissions to 777, and create a folder in this one using PHP, this subfolder can then not be deleted by my FTP. I'm assuming this is because it is owned by Apache not me. I can of course delete the subfolder with PHP ... and change its permissions.

    Now this made me think, perhaps I could chown() my folders to Apache, so I can have 777 permissions. This means I (and anyone else) could update files from PHP, but not from their usual account. Slightly safer, but still not good. Plus of course I will not be able to delete folders etc via FTP.

    But I may be thinking on the right steps.

    So ... I created a folder by PHP, then chmod() to 755. It worked (permissins had a T after them in FlashFXP (d-w-rwxr-T)- whatever that means) ... but now I can't delete this using PHP commands, system() commands within PHP or by FTP! I can't even use system() and chown to change ownership to my FTP account.

    Now to make things slightly harder, as well as needing to read/write/delete/modify files/folders in seperate folders which I may be able to solve using some method. I also need to be able to create folders in my public_html folder so people can go to www.mysite.com/subsite/

    I know this is a mess, but I am sure it's common, so can someone please help?

    Cheers

      I am pretty sure you can't chmod the root folder. Can you bury what you need deeper into the site?

      From the manual
      http://ca.php.net/manual/en/function.chown.php

      If you're on a shared *nix server, a directory created through mkdir() will not be assigned to you, but to the user that your host's server or php process is running under, usually 'nobody', 'apache' or 'httpd'.

      In practice, this means that you can create directories, even add files to them, but you can't delete the directory or its contents nor change permissions.

      It is therefore advised to create directories through PHP's FTP API. Here's a function I wrote:

      // create directory through FTP connection
      function FtpMkdir($path, $newDir) {

         $server='ftp.yourserver.com'; // ftp server
         $connection = ftp_connect($ftp_server); // connection
      
      
         // login to ftp server
         $user = "me";
         $pass = "password";
         $result = ftp_login($connection, $user, $pass); 

      // check if connection was made
      if ((!$connection) || (!$result)) {
      return false;
      exit();
      } else {
      ftp_chdir($connection, $path); // go to destination dir
      if(ftp_mkdir($connection,$newDir)) { // create directory
      return $newDir;
      } else {
      return false;

      }
      ftp_close($conn_id); // close connection
      }

      }

      Hope this comes in handy for someone.

      maybe this will be helplful as to why you can't seem to do some of what you need. you may need to change the approach and use an ftp script

        Write a Reply...