views:

185

answers:

5

I have a folder, and for all php files in that folder (or even better, in that folder or any folders within it) I'd like to make some changes to the php settings. Can I just place a php.ini file in that folder with those settings I'd like to change?

If so, any reason why this wouldn't be working for me? It's my own server.

Thanks!

edit: I'd like to be able to use a local php.ini file, as I've been able to do with several webhosts. Is this a possibility?

+1  A: 

You'll have to use a .htaccess file for that. There a section in the PHP manual about that:

http://php.net/manual/en/configuration.changes.php

For more general information on htaccess files you can read:

http://en.wikipedia.org/wiki/Htaccess

or

http://httpd.apache.org/docs/2.0/howto/htaccess.html

Techpriester
Are you sure I can't use local php.ini files? I am not able to use htaccess, as I'm on php 5.0, and I cannot upgrade as that could break some of my apps. (+1 btw for trying to help!)
Cam
.htaccess does not have anything with PHP to do. You can change PHP settings in .htaccess for both PHP 4 and 5. However, it dDO require that you are running Apache as your web server, as other web servers donät use .htaccess (but they may have similar solutions)
Emil Vikström
htaccess will work regardless of your PHP version. A "local php.ini" will not work because PHP only loads the main ini file and additional files that are configured in it.
Techpriester
That is only partially correct, Techpriester. http://www.php.net/manual/en/configuration.file.php
Joseph
@joseph: Could you be a little bit more precise about that? Do you mean that the location of the actually loaded ini file can depend on several things?
Techpriester
@Techpriester: I always understood that the php.ini can be partially overwritten by a local php.ini similar to the way htaccess files work. That is what I got from the link I mentioned above. However, it was not to be depended on since it required php and apache to be set up a specific way (not default) and therefore htaccess was the better way to go. I could be wrong as I have not had a reason or opportunity to test it out, but that is what I have understood it to mean.
Joseph
A: 

instead of modifying php.ini file for each folder you would be required to modify a .htaccess file. Keep the file in the folders with whatever setting you like. You cant do this with a php.ini file since changes in php.ini are considered server wide

sushil bharwani
A: 

You could also use ini_set(), if you wanted to do it in code.

chris12892
The settings must be set before the scripts run. This is not an option.
Cam
Ah, ok. Some applications that I am have, I have included a file in the boot loader to set the options to the required values, then continue with running the application. I don't know of any performance issues, but it does make the values a little bit easier to manage. Usually, I am just setting things like the upload limits and max execute times.
chris12892
Yeah. I do appreciate the response - this isn't what I need though :)
Cam
A: 

The .htaccess files are typically the best way to go for an Apache server. However, to answer your original question, yes you can set a php.ini file in every directory if you want. However, in order for it to work, PHP must be set to run as PHP-CGI. My guess is that you are running PHP as an Apache module.

See this link for reference on where PHP looks for php.ini and when it looks for it: http://www.php.net/manual/en/configuration.file.php

Joseph
+1  A: 

It looks like you're wanting to use per-directory php.ini files which are available as of PHP 5.3. If it's your own server, I'd like to think you're happy to keep up with the latest stable releases (currently 5.3.2). Back to ini files, to quote that manual page:

Since PHP 5.3.0, PHP includes support for .htaccess-style INI files on a per-directory basis. These files are processed only by the CGI/FastCGI SAPI. This functionality obsoletes the PECL htscanner extension. If you are using Apache, use .htaccess files for the same effect.

In addition to the main php.ini file, PHP scans for INI files in each directory, starting with the directory of the requested PHP file, and working its way up to the current document root (as set in $_SERVER['DOCUMENT_ROOT']). Only INI settings with the modes PHP_INI_PERDIR and PHP_INI_USER will be recognized in .user.ini-style INI files.

salathe
Brilliant - this didn't directly help solve the problem, but led me to a very similar solution. Thanks!
Cam