Writing local files via PHP

software development


Is it even possible to open and write to a local file under my dreeamhost account in PHP? My calls to fopen($filename, “w”) keep failing. I’ve set all of the write permissions to full, and I’m using the full absolute path to the new file. I really need this functionality within PHP, is there some other way to do it?

–Brad Barkhouse
Be a Contender!


It should work if you’ve set 777 (rwx for user/group/all) on the directory you’re trying to write to.

It’s way easier to do file manipulation in PHP when you use the CGI copy. Just name your script with a .pcgi extension instead of .php, or put an .htaccess file with this line in it:

AddType php-cgi .php

Then your PHP scripts will run as your user/group and you won’t have lots of nasty permission problems and big fat security holes.



Hmmm … I tried adding the AddType line, but now I can’t access necessary environment variables in the PHP code. I even edited my .bashrc, .bash_profile, and .htaccess files to include the variable settings, to no avail.

I basically want to include other files, via a specified base directory, like so:


But it won’t resolve the $BASEDIR var:

Fatal error: Failed opening required ‘/libs/php/UltraStats/UltraStatsManager.php’ (include_path=’.’)


–Brad Barkhouse
Be a Contender!


That kbase page is about CGI scripts, not about PHP scripts. Also, it’s talking about the script itself, and not about the directory / file you’re trying to write to.

Since PHP runs as the Apache user, it doesn’t run as your user or group, and thus doesn’t have permission to write to your files.