Sunday, 20 March 2011

PHP Error: Permission denied in Unknown at line 0 [FIX]

I was setting up a quick PHP test in my home directory today, but when I tried to load my file (http://localhost/~david/test.php) I encountered a strange error:

Warning: Unknown: failed to open stream: Permission denied in Unknown on line 0 Fatal error: Unknown: Failed opening required '/home/david/public_html/test.php' (include_path='.:/usr/share/php5/PEAR:/usr/share/php5') in Unknown on line 0

My code was pretty simple - no "include" or "require" statements anywhere, and perfectly valid syntax. This puzzled me for some time - it appeared as if PHP was trying to "require" my test file in itself. As it turns out, that wasn't too far from the truth.

This error was simply caused by Apache (and therefore PHP) not having read permissions to my PHP file. So it could tell that the file was there (otherwise I would get a 404 error), but when PHP tried to open the file for reading it failed, due to my file permissions (in this case, 007). To fix the problem, I simply had to run:

chmod 777 /home/david/test.php

After this my PHP code ran perfectly. It's worth knowing that this kind of error can occur - it might save a lot of Googling like I had to do!

Note: Normally I wouldn't change the permissions on a PHP file to 777 (everyone has full access, including write). However, in this case it was just a simple test page running on localhost, so I wasn't too worried about security.

1 comment:

Anonymous said...

Thank you for posting this!! I've had the same problem and i've been baffled for days as to why it wouldn't work!
Thank you!!!!