Getting around PHP safe mode to write to server. Is it possible?

随声附和 提交于 2019-12-30 11:14:31

问题


I have got the following problem since the server has safe mode turned on, and directories are being created under different users:

  1. I upload my script to the server, it shows as belonging to 'user1'. All it is doing is making a new directory when a new user is created so it can store files in it.
  2. New directory is created, but it belongs to 'apache' user.
  3. 'user1' and 'apache' are different users; and safe mode is turned on. So the php script cannot write to that newly created directory.
  4. Now I have a problem!

One solution is to turn off safe mode. Also, a coworker suggested that there are settings that can be changed to ensure the directories are under the same user as the script. So I am looking to see if latter can be done.

But I have to ask. Is there a programatical solution for my problem?

I am leaning to a 'no', as safe mode was implemented to solve it at the php level. Also the actual problem may seem like the directory being created under a different user, so a programatic fix might just be a band-aid fix.


回答1:


I've used this workaround:

instead of php mkdir you can create directories by FTP with proper rights.

    function FtpMkdir($path, $newDir) {
       $path = 'mainwebsite_html/'.$path;
       $server='ftp.myserver.com'; // ftp server
       $connection = ftp_connect($server); // connection


       // login to ftp server
       $user = "user@myserver.com";
       $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
             ftp_site($connection, "CHMOD 777 $newDir") or die("FTP SITE CMD failed.");
             return $newDir;
         } else {
           return false;
         }

         ftp_close($connection); // close connection
      }

  }  



回答2:


You might be able to turn safe mode off for a specific directory via a .htaccess file (if on Apache).

php_value safe_mode = Off

You might need to get your hosting provider to make this change for you though in the httpd.conf.




回答3:


I have had some success with setting the group bit of the upload directory to sticky. PHP can then create directories inside it and write to it.

http://en.wikipedia.org/wiki/Setuid#setuid_and_setgid_on_directories

chmod g+s directory



来源:https://stackoverflow.com/questions/252615/getting-around-php-safe-mode-to-write-to-server-is-it-possible

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!