Click here to Skip to main content
15,888,160 members
Articles / Programming Languages / PHP
Technical Blog

Have faith in your detective mind while dealing with php.ini and Apache2

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
24 May 2013CPOL4 min read 7.4K  
Stick to your detective mind no matter how complex the question becomes.

Look for the problem

 

More or less we all know that solving a problem in the programming world needs a lot detective work. And to my experience (mostly from holly wood movies) on detective operations there is some rule of thumbs.

  1. Follow the trail up to a reasonable ending
  2. Try to link the points

However last night I was caught up in a tedious problem with my apache server with php5. I needed to upload large files to the server – more than 10 MB. By default apache2 with php5 won’t allow you to upload more than 2MB files in the server.

Now to make it do the task you will have to do is modifying the php.ini file which normally resides in the path /etc/php5/apache2/. In this file there is a flag setting upload_max_filesize = 2M. I needed to set it to upload_max_filesize = 20M. So I did it and tried to upload a file using a php-script. A typical script of this kind can be found here. To find the upload failing reason check the $_FILES['userfilefield']['error'] value. If the value is 1 then it is the file size causing the problem. More of this error values are described here.

But to my surprise I found the value of $_FILES['userfilefield']['error'] to 1 which means it’s the file size causing the problem. I scratched my head a little while then started to think what could be the other problems. To find this out I started to trigger my detective mind and rollout a list of reasons that could cause this fail.

Detective question 1

Is the apache loading this php.ini file properly?

To find this I need to run a script say testup.php containing . These I will be able to see a list of flags loaded by apache2 and their values. To find out whether the php.ini is loaded or not check this.

Loaded Configuration File/etc/php5/apache2/php.ini

So it’s loading or at least started loading the file.

So the next question is –

Detective question 2

Has the upload_max_filesize flag value loaded in apache?

Check the that flag in the testup.php

upload_max_filesize2M2M

Bang!! It’s not loading the flag value properly. And that is why it’s failing to upload my large file.

Well why php.ini is not loading where I have changed the value myself and saved the file properly. So the big question is

Detective question 3

Why the upload_max_filesize flag is not loaded whereas the php.ini file has started loading?

Here at this point I was derailed from my detective rules and started to become impatient. I started to find out a patch on how to fix it with lots of googleing rather than finding the answer of the above question. With this mistake I have started lots of suffering which I could easily avoid if I have maintained the detective rules.

Likely solution 1:

One solution seemed most likely to solve my problem – that is to use the .htaccess file in the directory there the testup.php resides. In this .htaccess file you just will have to put this

php_value upload_max_filesize 20M
php_value post_max_size 22M

Then while accessing the testup.php file apache2 will automatically change the values of those flags. But to my ignorance, this will only work while apache2 have successfully loaded the php.ini file and in the file it has been explicitly told to do so. So very reasonably it didn’t help me either.

Likely solution 2:

The other solution seemed suitable is to use the “php -i | grep php.ini” and see there the php5 is loading the php.ini from which is “/etc/php5/cli/php.ini”. But here also to my ignorance this php.ini is nothing to do with apachi2. So very reasonably changing the flag value in this file had no effect on apache.

At the verge of my patience and back to the detective question

Why the upload_max_filesize flag is not loaded whereas the php.ini file has started loading?

Finally I came back to my detective question and started following my detective mind. Now I will have to check whether the apache2 has successfully finished loading the php.ini file. To check this I will have to check the log of the apache2 which is “/var/log/apache2/error.log” for my Ubuntu machine.

Solution comes automatically

To my surprise I found lots of error loading the php.ini as bellow.

PHP: syntax error, unexpected ‘&’ in /etc/php5/apache2/php.ini on line 110
PHP: syntax error, unexpected ‘&’ in /etc/php5/apache2/php.ini on line 110

Then I understood even apache2 had started loading the php.ini file, these errors did not let the php.ini finished loading successfully.

Now the life became easy, fix those errors in the php.ini file and restart the apache2. Then my large files were uploaded successfully with my testup.php script. What I had the problem with php.ini file is the value “Default Value: E_ALL & ~E_NOTICE”. Some how apache could not parse the “&”. Then I used “Default Value: E_ALL” instead of the previous value. But this new flag setting will introduce a new problem that it will not show you the compilation-errors on the page. Set the display_errors flag to On i.e. ”display_errors = On”. Finally solved the problem as I wanted. Sweet, isn’t it?

Bottom line: stick to your detective mind no matter how complex the question becomes.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
Sweden Sweden
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
-- There are no messages in this forum --