views:

93

answers:

4

Alright, so I have a problem. I need to create a folder to store compiled templates in php. I have an install script that tries to mkdir() an appropriate directory (compiled_templates).

Invariably it returns this warning.

Warning: mkdir() [function.mkdir]: Permission denied in /home/tchalvak/sites/quotesite/quotes/install.php on line 92

Running changing the mode of the folder that the compiled_template folder will be created within also errors out on the same permissions problem.

Is there no way to assure that you can create what amounts to a storage folder via php? You have to use an outside-php program just to ensure that a folder is created? How do other php developers deal with this issue?

Edit: This is an installable script, so the overall objective is to allow 10 different random people to install the script on their server. This is why "making sure that php has all these great necessary write permissions to the compiled_templates directory" isn't practical as a manual process, unfortunately.

Edit: Damn, same permission error when just trying to create a temp directory as well. Lame.

+3  A: 

If it's a temporary directory, create the directory in the system temp dir.

mkdir(sys_get_temp_dir() . 'example');

The general approach though is to simply ensure the user PHP is run as (e.g. apache) has write permissions where necessary.

enbuyukfener
+1  A: 

Normally we set up the system to have the appropriate permissions, if that isn't an option your script should fail with a nice descriptive error message and possible solution.

Since you mentioned that you want a temp folder maybe the tmpfile(), tempnam() and sys_get_temp_dir() functions may be useful for you.

Alix Axel
+1  A: 

I agree with putting the dir in tmp. To answer the more general question about dealing with this in installer script in general... You should set the perms up on the web root before you run the script and also make sure the perms are correct on any dirs within your package. With that said i generally try to avoid web installers and instead write installers for the CLI version of php. Of course you need ssh access on the box to do that but for me at least its very rarely the case that i dont.

prodigitalson
A: 

If using the system temp directory won't suit you, I've seen creative installers that ask for FTP credentials, and then use FTP to create directories and set permissions.

This may, however, be a bit too clever for it's own good.

Charles