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:


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:

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){


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:

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 24 days ago
Thanks Kerncy! I have just sent you a private message. Please check it.
georgefountain 24 days ago
Hey georgefountion, I think it a problem with your header('Content-Length: ' . filesize($filePath)) .More specifically the file path $filePath
Codeword 24 days ago
For experimentation just set Content-Length manually to a large value and see what happens header('Content-Length: 100000000000')
Codeword 24 days 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 24 days 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 24 days ago
I have updated my previous comment,Have you tried setting content-length manually to the file size of image?
Codeword 24 days ago
After this try to set zlib.output_compression =Off
Codeword 24 days 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 24 days 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 24 days ago
The mismatch error only occurs when requesting a non-cached image.
georgefountain 24 days 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 24 days ago
Can you verify that? I have just sent you a private message.
georgefountain 24 days ago
awarded to Codeword

Crowdsource coding tasks.

3 Solutions


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 :

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 21 days ago
PLease double check if all is fine with the links I previously provided
georgefountain 21 days 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 21 days ago
Thanks so much kerncy! Tip awarded :)
georgefountain 21 days ago
Thank you for the tip !
kerncy 20 days 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).


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

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

Thanks Goergefountain.
Codeword 20 days ago
View Timeline