Skip to Content
Course content

4: PHP Configuration with php.ini

Click on the "Edit" button in the top corner of the screen to edit your slide content.

You've probably had this happen: you're building a feature that lets users upload a profile picture or a PDF, and everything looks perfect in your code. You've got the form, the move_uploaded_file() logic, and the validation. But then you try to upload a 5MB file, and the script just... fails. Or worse, the $_FILES array is completely empty, and PHP doesn't give you a helpful error message. It's one of the most frustrating "silent" failures in the language.

The illusion of control with ini_set

When I first started with PHP, my instinct was to fix everything inside the script. I figured if I needed a larger upload limit, I'd just tell PHP to change it right at the top of my file. I'd write something like ini_set('upload_max_filesize', '10M'); and wonder why it did absolutely nothing. It feels logical—you're in the code, you see the setting, you change it. But here is the catch: ini_set() only works for settings that are changeable at runtime.

PHP categorizes its configuration settings. Some can be changed anywhere (PHP_INI_ALL), some only in your .ini or .htaccess files (PHP_INI_PERDIR), and some only in the master php.ini (PHP_INI_SYSTEM). The upload limit is a PHP_INI_PERDIR setting. By the time your script actually starts executing and hits that ini_set() line, the PHP engine has already received the request, seen that the file was too large, and discarded it. You're trying to change the rules of the game after the whistle has already blown.

Changing the engine's blueprints

The real way to handle this is by editing the php.ini file itself. Think of this file as the blueprints for the PHP engine. It tells PHP how to behave before a single line of your application code ever runs. Instead of fighting the environment, you configure the environment to support your application.

To fix the upload issue, you have to find where your php.ini actually lives—which is often a scavenger hunt depending on whether you're using Apache, Nginx, or a built-in server—and modify the actual directives. You'll want to look for upload_max_filesize and post_max_size. I always remind people that post_max_size must be larger than or equal to upload_max_filesize, because the file upload is just one part of the total POST request. If you set the file limit to 10MB but leave the POST limit at 8MB, you're still going to crash.

; In php.ini
upload_max_filesize = 10M
post_max_size = 12M
memory_limit = 128M

After you save these changes, you have to restart your web server (like Apache or PHP-FPM). This is the part that trips up a lot of developers. Since php.ini is loaded into memory when the process starts, the engine won't see your changes until it's forced to reload that file.

When global changes bite back

Now, you might be thinking, "Why not just crank every limit up to the max so I never have to deal with this again?" I've seen this in production environments, and it's a recipe for a crash. If you set memory_limit to 2GB because one specific reporting script needs it, you've just given every single request on your server permission to eat 2GB of RAM. If you get ten concurrent visitors, your server will swap to disk and grind to a halt.

The trade-off here is between convenience and stability. The php.ini is a global hammer. If you're on a shared host where you can't touch the master file, you might be able to use a .user.ini file in your directory to override specific settings. But as a general rule: keep your global settings conservative and only increase them to the minimum required for your app to function. It forces you to write more efficient code and prevents a single buggy loop from taking down your entire infrastructure.




📋 Practical Task

Expanding the File Upload Ceiling

Your goal is to move beyond runtime configuration and successfully modify the PHP environment to allow larger file uploads.

  • Create a file named info.php containing and run it in your browser. Search the page for "Loaded Configuration File" to find the exact absolute path to your active php.ini.
  • Open that php.ini file in a text editor with administrative privileges.
  • Locate the upload_max_filesize and post_max_size directives. Change them both to 20M.
  • Restart your web server (e.g., sudo service apache2 restart or restarting your local development environment).
  • Refresh your info.php page and verify that the "Local Value" for both settings now reflects 20M.
Rating
0 0

There are no comments for now.

to be the first to leave a comment.