2

I am having trouble using fopen() to create a text document for later use as a cookie file.

I have read the documentation for this function, but to no avail.

Notes:

  • Ubuntu
  • read / writable ("w+")
  • I have tried several storage locations including:
    • /home/jack/Desktop/cookie
    • /var/www/cookie
    • /home/jack/Documents/cookie

PHP

echo "debug";
echo "\r\n";

$cookie = fopen("/home/jack/Documents/cookie", "w+");
fclose($cookie);

if(!file_exists($cookie) || !is_writable($cookie))
{
    if(!file_exists($cookie))
    {
        echo 'Cookie file does not exist.';
    }
    if(!is_writable($cookie))
    {
        echo 'Cookie file is not writable.';
    }

    exit;
}

Result

  • file is not created
  • Output to browser: debug Cookie file does not exist.Cookie file is not writable.

Other Fun Facts

  • I have tried using fopen(realpath("/home/jack/Documents/cookie"), "w+")
  • echo "\r\n" gives a space. Why not a newline?

I believe the problem must be something to do with my permissions to create the file, but I have no problem "right-click" creating the text document on the Desktop.

THIS WORKS THIS WORKS THIS WORKS THIS WORKS THIS WORKS THIS WORKS THIS WORKS

echo "debug";
echo "\n";

$jack = "jack";
$cookie = "/home/jack/Documents/cookie";
touch($cookie);
chmod($cookie, 0760);

if(!file_exists($cookie) || !is_writable($cookie))
{
    if(!file_exists($cookie))
    {
        echo 'Cookie file does not exist.';
    }
    if(!is_writable($cookie))
    {
        echo 'Cookie file is not writable.';
    }

    exit;
}

fclose($cookie);

THIS WORKS THIS WORKS THIS WORKS THIS WORKS THIS WORKS THIS WORKS THIS WORKS

Instead of fopen()..

  • touch() to create
  • chmod() for permissions

I also added user name jack to www-data group.

  • chmod($path, 0760) group read / write

Reference

  • chmod() octal values here.
ma77c
  • 1,052
  • 14
  • 31

3 Answers3

3

Look at the documentation for file_exists again. It does not take a file handle as an argument, it takes a string filename. The same is true for is_writable. Even if it did, you are opening the file handle and then immediately closing it, so I'm not sure why you're trying to use the file pointer at all after it's been closed.

You may be correct in that you have improper permissions set, but I would start here, first.

Also, if you're only trying to create the file, you may look into using the touch method, instead:

if( touch( $filename ) ) {
    // It worked!
} else {
    // It didn't work...
}
Jeff Lambert
  • 24,395
  • 4
  • 69
  • 96
1

It looks like a permission issue. What user is PHP running as? It's likely running as www-data or something similar. You should make sure that the folders you are trying to write to are writable by either the user or group that PHP is running as. If you created those folders while logged in a jack, they probably belong to jack:jack and are not accessible by www-data:www-data.

You can also add jack to the www-data group, to make things a bit easier for development.

Brian H.
  • 505
  • 2
  • 12
1

The web server is not executing as your user. touch /home/jack/Documents/cookie && chmod 777 /home/jack/Documents/cookie to allow the web server user to access the file.

Note this is BAD in production environments.

PaulProgrammer
  • 16,175
  • 4
  • 39
  • 56