开发者

PHP rename() Ignoring Permissions?

开发者 https://www.devze.com 2023-04-08 00:37 出处:网络
Environment PHP -V output: PHP 5.3.5-1ubuntu7.2 with Suhosin-Patch (cli) (built: May2 2011 23:00:17) Copyright (c) 1997-2009 The PHP Group Zend Engine v2.3.0, Copyright (c) 1998-2010 Zend Technologi

Environment

  • PHP -V output: PHP 5.3.5-1ubuntu7.2 with Suhosin-Patch (cli) (built: May 2 2011 23:00:17) Copyright (c) 1997-2009 The PHP Group Zend Engine v2.3.0, Copyright (c) 1998-2010 Zend Technologies
  • cat /etc/issue output: Ubuntu 11.04
  • Apache2 -V ouput: Server version: Apache/2.2.17 (Ubuntu) Server built: Sep 1 2011 09:31:14
  • Browser About output: Firefox 6.0.2

PS -AUX Ouput

root      2943  0.0  0.3 206420 12428 ?        Ss   Sep19   0:20 /usr/sbin/apache2 -k start
www-data 18658  0.0  0.2 208552 11096 ?        S    Sep25   0:00 /usr/sbin/apache2 -k start
www-data 18659  0.0  0.3 208976 12036 ?        S    Sep25   0:00 /usr/sbin/apache2 -k start
www-data 18660  0.0  0.3 210532 12476 ?        S开发者_开发百科    Sep25   0:00 /usr/sbin/apache2 -k start
www-data 18661  0.0  0.3 210276 11820 ?        S    Sep25   0:00 /usr/sbin/apache2 -k start
www-data 18662  0.0  0.2 206948 10236 ?        S    Sep25   0:00 /usr/sbin/apache2 -k start
www-data 20037  0.0  0.3 208976 12128 ?        S    08:22   0:00 /usr/sbin/apache2 -k start
www-data 20039  0.0  0.3 209132 11748 ?        S    08:23   0:00 /usr/sbin/apache2 -k start
www-data 20120  0.0  0.3 209004 12000 ?        S    09:04   0:00 /usr/sbin/apache2 -k start

File Permissions

drwxr-xr-x 2 www-data www-data   4096 2011-09-26 15:24 .
drwxr-xr-x 4 www-data www-data   4096 2011-08-26 11:31 ..
-rw-r--r-- 1 root     root     161976 2011-08-26 16:26 market.txt
-rw-r--r-- 1 root     root          0 2011-09-26 14:55 test1.txt
-rw-r--r-- 1 root     root          0 2011-09-26 14:55 test2.txt
-rw-r--r-- 1 root     root          0 2011-09-26 14:55 test3.txt
-rw-r--r-- 1 root     root          0 2011-09-26 14:55 test4.txt
-rw-r--r-- 1 root     root          0 2011-09-26 15:02 test5.txt

Code

rename($file, "$dest/$file");

Question

When I run the above code on the files listed in the File Permissions section above, it properly moves the file from its current location to a new location and removes the original. How is this possible when apache2 is running as www-data and the files are owned by root and only have read access for non-root users? On the PHP documentation it says:

Warnings may be generated if the destination filesystem doesn't permit chown() or chmod() system calls to be made on files — for example, if the destination filesystem is a FAT filesystem.

Does rename() call either of those system functions during the process? If so, why? Not that it matters anyway as www-data should not be able to chown/chmod a file owned by root anyway.

Can anyone explain to me how this is occuring?

Additional Information

  • I have tried this with the PHP script owned by root and by www-data and it works.

I tried to provide as much pertinent info as possible but let me know if you need anything else.


Moves don't "remove" originals, unless the move takes place across filesystem boundaries. Within a single filesystem, a move simply rewrites the relevant directory entries so it APPEARS that you've copied/deleted the file, but all you've done is a bit of housekeeping. Since www-data owns the directories in question, it can rewrite the directory entries representing those files all it wants, and never touch the actual files.


When moving files you are not editing the files themselves, but rather the directory they are part of. In your case that directory is owned by www-data (the apache process)

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号