0

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?

2 Answers 2

6

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.

Sign up to request clarification or add additional context in comments.

1 Comment

to anyone except Alnitak: If your mind is having a hard time understaining the umask value (like mine did), remember that the permission bits files are created with is the inverse of the umask (and removing the execution bits after the inversion). So with that umask, newly created files would be 0400 (or -r-------- if you prefer that way).
0

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.)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.