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 createchmod()
for permissions
I also added user name jack to www-data group.
chmod($path, 0760)
group read / write
Reference
chmod()
octal values here.