I'm getting this error when I'm trying to create a login page:

Warning: file_put_contents(/users/userlist.txt): failed to open stream: Permission denied in /Library/WebServer/Documents/register_draft.php on line 17

Warning: file_put_contents(/users/passlist.txt): failed to open stream: Permission denied in /Library/WebServer/Documents/register_draft.php on line 18

Warning: file_put_contents(/users/pinlist.txt): failed to open stream: Permission denied in /Library/WebServer/Documents/register_draft.php on line 19

Warning: Cannot modify header information - headers already sent by (output started at /Library/WebServer/Documents/register_draft.php:17) in /Library/WebServer/Documents/register_draft.php on line 20

I have created a folder I need to put all the users in called "users" in my /Library/Webserver/Documents/

Here's my code:

$file1 = '/users/userlist.txt';
$file2 = '/users/passlist.txt';
$file3 = '/users/pinlist.txt';
$user = usermail;
$pass = password;
$pin = pi;

$safeUser = testUserInput($user);
$safePass = testUserInput($pass);
$safePin = testUserInput($pin);

if ($safeUser != "unsafe" && $safeUser != "empty" && $safeUser != "spaces") {
if ($safePass != "empty" && $safePass != "unsafe" && $safePass != "spaces") {
    if ($safePin != "empty" && $safePin != "unsafe" && $safePin != "spaces") {
        $hashedPass = password_hash($safePass, PASSWORD_BCRYPT);
        file_put_contents($file1, $safeUser . "<br>", FILE_APPEND | LOCK_EX);
        file_put_contents($file2, $hashedPass . "<br>", FILE_APPEND | LOCK_EX);
        file_put_contents($file3, $safePin . "<br>", FILE_APPEND | LOCK_EX);
        header("Location: ../users"); //change this to direct user to login page, account successfully created
}     else {
header("Location: ../users"); //change this to direct user back to     registration page with error prompt

function testUserInput($x) {
$test = strpbrk($x,"<>/\;()%-+.=,");
if ($test != false) {
    $x = "unsafe";  
} elseif (strlen($x) == 0) {
    $x = "empty";
} elseif (ctype_space($x)) {
    $x = "spaces";
return $x;

If you see any other problems with the code, please let me know. Thanks.

  • php runs as the _www user normally on OS X, so does _www have write and execute permissions for the relevant directory? Also paths starting with '/' are absolute and refer to root, so you are referring to the /Users directory that OS X uses to store home directories by default, which doesn't seem like what you want. Apr 24, 2015 at 21:56

1 Answer 1


Try these please:

  1. Run your commands as sudo whenever you get such messages
  2. For your document root, execute: sudo chown _www:_www /path_to_docroot/

Please share the results and we can debug further if this does not solve the issue.


Not the answer you're looking for? Browse other questions tagged or ask your own question.