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.
The ftruncate() function truncates an open file to the specified length.
use PHP tokenizer function strtok() in a loop.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With