Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why would php://input truncate?

Tags:

php

upload

I have the line $input = fopen( 'php://input', 'r' ); in my code for uploads.

When the file is done uploading, I check against $_SERVER['CONTENT_LENGTH'] to see everything was sent as expected. Recently the CONTENT_LENGTH and the size of my upload file started to not match randomly ( About once every 20 minutes with people uploading very frequently ).

I logged and saved the mismatches and found that some files were unable to be opened, while some downloaded and were cut off. One I opened in Photoshop even warned that the document was damaged.

Is there any server config I should be looking for that may have been changed?

I tried replicating by shutting browser tabs and things of that nature but still would not replicate the error.

We are uploading via an AJAX POST. The POST body is the upload source.

like image 551
Dave Stein Avatar asked Aug 26 '13 14:08

Dave Stein


People also ask

What is truncate PHP?

The ftruncate() function truncates an open file to the specified length.

How can I truncate a string to the first 20 words in php?

use PHP tokenizer function strtok() in a loop.

Is PHP Input safe?

If you do something like exec(php://input) (psuedocode), you're going to have a bad day. If instead you just read the input stream and properly handle the data you're getting, you're fine. There's nothing inherently secure or insecure about the input stream. It's what you do with it that matters.


1 Answers

It sounds to me like a load/networking issue. Either there is a latency issue/timeout issue where the users are trying to upload and your server is not responding.

Usually it's not the AJAX to blame, rather the server (maybe not even PHP) . I think there are drops in some places of the packets coming to the server. It can be limitation in the datacenter (Ex. bandwith cap.) or limitation on the server itself.

We optimize our server using the following configuration. This should work on any linux distro:

Edit /etc/sysctl.conf file. Add the following lines in the end:

net.ipv4.tcp_wmem= 10240 87380 12582912
net.ipv4.tcp_rmem= 10240 87380 12582912
net.core.rmem_max=12582912
net.core.wmem_max=12582912
net.ipv4.tcp_max_syn_backlog=100000

You can use the following manual from IBM to better understand how to tune up your linux: http://www.redbooks.ibm.com/redpapers/pdfs/redp4285.pdf

Also,

I would log the client's browser version. It might be that the format sent to your server depends on the AJAX is different from one browser to the other. Since you didn't specify the AJAX code it's hard to assume what's the AJAX doing to upload the files.

like image 189
Daniel Avatar answered Nov 12 '22 12:11

Daniel