PHP Image Generation - ERR_CONTENT_LENGTH_MISMATCH
New here? Learn about Bountify and follow @bountify to get notified of new bounties! x

I am creating images dynamically with PHP, and when serving the images the image loads half way and then stops loading and the image is replaced by nothing (as if it was an unavaiable image), and on console I see this error:

net::ERR_CONTENT_LENGTH_MISMATCH

1) I added set_time_limit(60) to my PHP script, but it doesn't seem to do anything.

2) These are the headers that console is showing:
https://pastebin.com/S7HLLXLR

3) I have a cache system. Whenever the image is generated for the first time (fresh), this error occurs. Whenever the image is served from cache, this error does not occur.

The code logic is as follows:

If( cache not exists){
    createCache();
}

renderImageFromCacheFolder()`

This means that in both cases (wherever cache is fresh or not), the same output function is used.

4) This is my PHP code for renderImageFromCacheFolder:
https://pastebin.com/x7q02ukL

Please help me solve this bug! Thank you

Do you have your server access and error log that is generated when you request your url ? It may help to determine the root cause of your issue.
kerncy 3 months ago
Thanks Kerncy! I have just sent you a private message. Please check it.
georgefountain 3 months ago
Hey georgefountion, I think it a problem with your header('Content-Length: ' . filesize($filePath)) .More specifically the file path $filePath
Codeword 3 months ago
For experimentation just set Content-Length manually to a large value and see what happens header('Content-Length: 100000000000')
Codeword 3 months ago
More specifically set the content-length manually to the file size of the image, and if it works there is a problem with your filesize
Codeword 3 months ago
Thanks @CodeWord. I just tried adding $filePath = self::getImagesCacheFolderPath(). $fileName . ".". $fileExtension; echo $filePath; exit(); ..and the filepath is correct. It points to a real image.
georgefountain 3 months ago
I have updated my previous comment,Have you tried setting content-length manually to the file size of image?
Codeword 3 months ago
After this try to set zlib.output_compression =Off
Codeword 3 months ago
I think the error is beacuse the as zlib compression is on so the filesize of image outputed by browser is not same as the actual filesize as calculated by filesize function and so the Mismatch error.
Codeword 3 months ago
But then, how can you explain that the code returns no error when giving a cached image and when generating one it gives an error?
georgefountain 3 months ago
The mismatch error only occurs when requesting a non-cached image.
georgefountain 3 months ago
As you said, "code returns no error when giving a cached image and when generating one it gives an error". I think this may be explained like this when the first time the images loads, due to zlib compression file sent to the browser mismatch the actual file calculated by filesize function.But when the image is in cached then this piece of code runs if($notModified){ header("HTTP/1.1 304 Not Modified"); exit; } runs and the code exits before it can send the header this time and so the error doesn't occur.
Codeword 3 months ago
Can you verify that? I have just sent you a private message.
georgefountain 3 months ago
awarded to Codeword
Tags
PHP

Crowdsource coding tasks.

3 Solutions


Hi,

For a trial, you can add the function "clearstatcache();" just before the call to "header('Content-Length: ' . filesize($filePath));"

From what I saw, the filesize uses a cached version of the data stored. If the file already exists it may use the invalid length as described here : https://www.w3schools.com/php/func_filesystem_filesize.asp

Hi Kerncy! Thanks for your help. Codeword privately suggested me the same solution you did just hours before you posted. I just added clearstatcache() before calling filesize(). Could you double check if everythign works? If so, I will award you a TIP, since you both had the same solution. Thanks!
georgefountain 3 months ago
PLease double check if all is fine with the links I previously provided
georgefountain 3 months ago
Hi, I checked the link you gave me by private message, and it's ok for me. I tested a few different colors, and PNG always loads well in Firefox, Chrome and IE.
kerncy 3 months ago
Thanks so much kerncy! Tip awarded :)
georgefountain 3 months ago
Thank you for the tip !
kerncy 3 months ago

Do you have access to php.ini / php_info(); In my experiences this is usually caused by a mismatch with php's internal maximum memory size and the file's max size.

What are your max allocation for php variables and do you have access to setting these outside of php function calls (which simply aren't usually enough)?

As for code completion/creation do you have access to doing this outside of cache or is write access a requirement you're stuck with? (Far from best practice).

Sincerely,
K

Thanks uhbandduh. The other developers were able to resolve it. Thanks anyways!
georgefountain 3 months ago
Winning solution

Hey georgefountain, I have tested just now.it's working correctly now.
Thank you

Thanks Goergefountain.
Codeword 3 months ago
View Timeline