Treehouse

Set php.ini Values Using .htaccess

By on  

Did you know that you can set php.ini values right inside the .htaccess file? It's actually very easy.

The .htaccess Code

#format
php_value setting_name setting_value

#example
php_value  upload_max_filesize  10M

Of course you could simply place these in the .htaccess file, but .htaccess is a viable alternative if your host doesn't allow you to touch the php.ini file.

ydkjs-2.png

Recent Features

  • Introducing MooTools Templated

    One major problem with creating UI components with the MooTools JavaScript framework is that there isn't a great way of allowing customization of template and ease of node creation. As of today, there are two ways of creating:

  • CSS vs. JS Animation: Which is Faster?

    How is it possible that JavaScript-based animation has secretly always been as fast — or faster — than CSS transitions? And, how is it possible that Adobe and Google consistently release media-rich mobile sites that rival the performance of native...

  • 6 Things You Didn’t Know About Firefox OS

    Firefox OS is all over the tech news and for good reason:  Mozilla's finally given web developers the platform that they need to create apps the way they've been creating them for years -- with CSS, HTML, and JavaScript....

Incredible Demos

  • Event Delegation with MooTools

    Events play a huge role in JavaScript. I can't name one website I've created in the past two years that hasn't used JavaScript event handling on some level. Ask yourself: how often do I inject elements into...

  • Styling CSS Print Page Breaks

    It's important to construct your websites in a fashion that lends well to print. I use a page-break CSS class on my websites to tell the browser to insert a page break at strategic points on the page....

  • pointer Media Query

    This media query has not yet been implemented and is currently only a proposed spec. I've written this post to raise awareness of it and get general thoughts about it. As more devices emerge and differences in device interaction are...

Discussion

  1. I’ll be needing something like that soon enough, thx for the tip :)

  2. Yeah, but this is not worked if PHP working as FastCGI :)

  3. Another option is setting it in PHP – using the ini_set() function.

  4. huh @guard?

    So this could work for httpd.conf’s values too? Or this one cannot be overridden?

  5. I actually used this just the other day. I was trying to use the “Import” feature of WordPress to import a bunch of posts and crap from an “Export” of another WordPress blog (XML File). WordPress was telling me there was a piddly 2MB limit. But apparently, this isn’t a limitation of WordPress but rather set by PHP running on the server. I upped it, but apparently it maxed at 8MB on my server, which was 2MB too small for my 10MB file. Booooo.

  6. I’ll remember that one, thanks !

    .htaccess has a lot of capabilities, do you know if there’s a reference for it, or a listing of the possibilities somewhere ?

  7. @Ahmed: php_flag and _php_value can be written in httpd.conf in FastCGI, but not in .htaccess file. if PHP is running as module, you can do anything with .htaccess

  8. @Chris Coyier: The upload limit is the one I change the most.

    @Antoine Leclair: I haven’t found one great one — Google usually yields the best results.

  9. Another way to do it is to use PHP’s native ini_set() function

  10. fred

    Readers may be interested by reading this page on the PHP documentation : How to change configuration settings. That is said you can also use the ini_set php function, which is an alternative to the .htaccess method.

    @Antoine Leclair: There is always the official documentation. You can find it for the last version of Apache, and you have a .htaccess tutorial.

  11. Consider reading the following, to get better understading of .htaccess and how it works. It really depends on your httpd.conf what you can do with the .htaccess file.

    http://httpd.apache.org/docs/2.2/howto/htaccess.html
    http://httpd.apache.org/docs/2.2/mod/core.html#allowoverride

  12. Wow that’s exactly what I needed to simplify my life. Thank you!

    Of course you could simply place
    these in the .htaccess file, but
    .htaccess is a viable alternative if
    your host doesn’t allow you to touch
    the .htaccess file.

    Maybe you meant httpd.conf? Or php.ini?

  13. divyesh

    hi,
    i am using mootools ans jquery and ligthbox in my project. but i have not access jquery with lightbox.

    http://www.immobelgica.be/en/search/apartment/for-sale/belgium/east__flanders/aalst/38548/5/

    in above url i have user jquery for thumb image move on mouse hover and when click on image that time i user lightbox for vieew image. that is ok but i also use lightbox for email to friend and page is not open in middle and same window like image.

    please reply me on my mail..

    have a nice time

    Thank for help..

  14. I am not an experienced coder. I have access to the .htaccess file. What exactly do I need to put in the file to increase my Upload file size limit? I’d greatly appreciate any help. Thank you all!

  15. wladia viviani: you can move items into your apache’s httpd.conf file – but that means the changes are only picked up on Apache’s restart. However if things won’t change often then it’ll save IO and processing on each request

    erik neuwirth: php_value upload_max_filesize 10M as in the example right at the top :)

    I know “everybody” knows this but it’s good to have a dev environment that’s the same as your live environment but enable php errors to be seen. What we do is simply have a dev htaccess file that contains


    # Eror Handling:
    php_flag display_errors on
    php_flag display_startup_errors on

    Then we can see any fallout on dev before live just swallows the problems (we don’t want to burden the user with an unset variable being erroneously used!)

  16. Hi. Thank for help. useful.

  17. my server blocked my email sending options and i need to add this line extension=php_openssl.dll
    to php.ini

    so can i just go to .htaccess and add it??????????/

  18. [...] obviously organized to me… I felt like I was being put through loops), and found a link to this post on David Walsh’s Blog. While this did not really address my question, I clicked the [...]

  19. Jomo

    Thanks! This post still showing up at the top of the google search all these years later — and the advice is still good.

  20. [...] : davidwalsh.name Tags: Setting php.ini dari .htaccess [...]

  21. It helped me, as i was stuck with this value because of a long form for translations purposes in Prestashop. When added that line in htaccess it just displayed the form correctly.
    Thank you man

  22. [...] Setting PHP.ini in .htaccess couldn’t be easier. I took this example from this article. [...]

  23. I tried this on my site and the results look good. Thanks for the tips.

  24. mccbala

    Thanks for this!

  25. Umair

    I am getting this error. ini_restore() has been disabled for security reasons

    How to change it through .htaccess file? and Where to put the code in the .htaccess file?

  26. I have been in tech support for years and if there is one thing I have learned, dont mess with what you dont understand when it comes to server settings. Apache is like a very muscular little girl. Extremely powerful but needs her hand held. Slight errors will bring your site to its knees. Just know what you are doing, dont go playing about in there. Its not a line a code that messes up a part of your site. Its how your server refrences files. Like all files. It wont show up in an error log if you mess it up it will just straight bring your site down. Make sure you create a backup before any tamperings. Just saying.

Wrap your code in <pre class="{language}"></pre> tags, link to a GitHub gist, JSFiddle fiddle, or CodePen pen to embed!

Use Code Editor