Configure memory usage for php

From PGVWiki
Jump to navigation Jump to search

Configure memory usage

That is the right place to set the memory limit in PGV's configuration. After setting it there, you need to check phpinfo from the config menu to see if your hosting provider's setup permits it to take effect. In most cases, the lower of the two limits (the one set in php.ini and the config of the running script) controls.

Don't assume that your hosting provider's limit is unchangeable. If you can't adjust the memory limit through the PGV config, contact them, and ask whether php.ini is customer-accessible, and if not, ask them to change the mem limit for you.

I work on sites hosted on servers from different providers, and their policies and procedures regarding php.ini vary widely. The one I use the most has php.ini in a peculiar location in the directory tree, to slow down hackers, and don't publish that location in their online help pages for obvious reasons, but were happy to tell me how to get to it. The provider which hosts my phpGedView site does not permit customer edits of php.ini, but bumped my php memory limit up to 16 and then to 32 on my request, with no complaint. Even with PGV 3.3.7, the speed boost was dramatic, and a transient and erratic problem I had with MySQL stopping, sometimes leaving a table locked, vanished.

Some hosts do not enable the memory limit (windows binaries usually don't have it enabled) which may be why you aren't seeing it in the phpinfo.

With Apache it is also possible to change php.ini settings in a .htaccess file. You can try creating a blank .htaccess file and put it in your phpgedview directory. Then put the following line in it: php_value memory_limit 32M

Though PHP presents a very versatile and user friendly interface for handling file uploads, the default installation is not geared for working with files in excess of 2 Mega Bytes. This article will help you configure your PHP engine for handling such large file transfers. The php.ini File

Special Note:

You could be wasting bandwidth, If your file upload page is nothing more than an HTML form.

Most browsers just ignore the MAX_FILE_SIZE hidden field and size limits are checked only after the data has been sent over the wire.

Our applet saves your bandwidth by imposing client side restrictions.

All the configuration settings for your installation are contained in the php.ini file. Sometimes these setting might be overridden by directives in apache .htaccess files or even with in the scripts themselves. However you cannot over ride the settings that effect file uploads with .htaccess directives in this way. So let's just concentrate on the ini file.

You can call the phpinfo() function to find the location of your php.ini file, it will also tell you the current values for the following settings that we need to modify

  • file_uploads
  • upload_max_filesize
  • max_input_time
  • memory_limit
  • max_execution_time
  • post_max_size

The first one is fairly obvious if you set this off, uploading is disabled for your installation. We will cover the rest of the configuration settings in detail below.

upload_max_filesize and post_max_size

Files are usually POSTed to the webserver in a format known as 'multipart/form-data'. The post_max_size sets the upper limit on the amount of data that a script can accept in this manner. Ideally this value should be larger than the value that you set for upload_max_filesize.

It's important to realize that upload_max_filesize is the sum of the sizes of all the files that you are uploading. post_max_size is the upload_max_filesize plus the sum of the lengths of all the other fields in the form plus any mime headers that the encoder might include. Since these fields are typically small you can often approximate the upload max size to the post max size.

According to the PHP documentation you can set a MAX_UPLOAD_LIMIT in your HTML form to suggest a limit to the browser. Our understanding is that browsers totally ignore this directive and the only solution that can impose such a client side restriction is our own Rad Upload Applet


When the PHP engine is handling an incoming POST it needs to keep some of the incoming data in memory. This directive has any effect only if you have used the --enable-memory-limit option at configuration time. Setting too high a value can be very dangerous because if several uploads are being handled concurrently all available memory will be used up and other unrelated scripts that consume a lot of memory might effect the whole server as well.

max_execution_time and max_input_time

These settings define the maximum life time of the script and the time that the script should spend in accepting input. If several mega bytes of data are being transfered max_input_time should be reasonably high. You can override the setting in the ini file for max_input_time by calling the set_time_limit() function in your scripts.

Additonal Comments

Apache Settings

The apache webserver has a LimitRequestBody configuration directive that restricts the size of all POST data regardless of the web scripting language in use. Some RPM installations sets limit request body to 512Kb. You will need to change this to a larger value or remove the entry altogether.

Other Options

If you expect to handle a large number of concurrent file transfers on your website consider using a perl or java server side component. PHP happens to be our favourite web programming language as well but perl and Java are just slightly ahead when it comes to file upload.

Most installations of perl as an apache module can accept in excess of 32 megabytes out of the box. Compare this against the 2MB default for PHP. The downside is that perl coding takes just a bit more effort than PHP but it's worth it. You can try our sample scripts to get started.