How to fix – PHP Warning: Unknown: POST data can’t be buffered; all data discarded in Unknown on line 0

So you have a PHP website and out of sudden, when users send POST requests or upload files, you get the error message:

PHP Warning: Unknown: POST data can't be buffered; all data discarded in Unknown on line 0

Well there are different reasons why you may get such an error. This articles goes through the common causes and solutions for this error.

Understanding the error

The error says that PHP was unable to buffer the post data, but it doesn’t really say why. Looking at the code, we see that the actual cause of the error is that the number of bytes written to the buffer is different to the number of bytes read from the input. Unfortunately if the logs don’t tell us anything additional, it is just outputting the symptom not the cause.

To make things worse, it is a warning a not a real error, meaning that your code will continue the flow on a corrupted state.

Possible causes to the error and how to fix them

Permissions on the temporary folder

The most likely cause for this error is that the user running the PHP process doesn’t have the right to save a file in the temporary folder.

Usually if the problem is in the temporary folder, you should also see the warning message:

Warning: File upload error - unable to create a temporary file in Unknown on line 0

The temporary folder for file uploads is defined by the php.ini directive upload_tmp_dir. When empty it uses the system default temporary directory (in Linux usually /tmp).

You can type this in the command line to find out what is the temporary dir.

php -r 'echo ini_get('upload_tmp_dir') ? ini_get('upload_tmp_dir') : sys_get_temp_dir();'

Once you know the folder you can change the permissions with chmod (in the below example for the /tmp folder)

chmod -R 777 /tmp

Out of disk space or quota

Similar to the error above, if PHP is receiving big post requests and there is not enough capacity for the server or the user to save a temporary file, you should get the same warning as above. To check the available space in the temporary folder you can try the command diskfree (df). The example below assumes that the temporary folder is /tmp

$ df /tmp
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/sda3 4062912 76344 3760472 2% /tmp

If you are out of disk space, well… get more space 🙂

Out of memory

If your process runs out of memory, you might get the same error. In this case, an out of memory message should precede the POST warning.

You have to check if the server (or virtual machine) is running out of memory or if the PHP reached its memory limit. PHP memory limits are configured by the directive memory_limit.

Upload file limits

PHP has several directives to control how big files can be and how many files an user can upload. If you hit any of these limits you might receive this error messages. You can adjust the upload limits through the php.ini directives:

Other possible errors

Some other documented cases:

Leave a comment

Your email address will not be published. Required fields are marked *