PHP: Create read only file without chmod()
I have chmod() disabled on开发者_运维技巧 my server, but I still want users to be able to create read-only files via PHP, is there a way to do so?
You can't, since it's impossible to put anything in a read-only file...
EDIT actually, there is a way:
<?php
$u = umask(0377); // disables --wxrwxrwx permissions
$f = fopen("test", "w");
umask($u);
fwrite($f, "this is a test\n");
fclose($f);
?>
% php foo.php
% ls -l test
-r-------- 1 xxx xxx 14 19 May 10:27 test
% cat test
this is a test
The umask
manipulation allows you to create a read/write file descriptor, even though the underlying directory entry is read-only.
As workaround, you could first store the files using file_put_contents()
, and then use ftp_chmod()
to change the file permissions.
The other option would be via the commandline, but if chmod
is disabled then exec
is unlikely to work either. And if your hoster disabled both, then there likely is a reason for that. (If only it is to reduce support costs.)
精彩评论