Fix disabled readfile function in PHP
Recently i had a problem with my blog. Actually the problem was on the server sever side where my blog hosted. I use a theme that require or use timthumb plugin. Maybe you already knew, i used it to generate and display thumbnail image, pretty good. The problem is the timthumb plugin use readfile function to read the image file and i dont know for sure why, my hosting service administrator just listed it as a disabled PHP function.
I do the ritual, search for a solution, and the only alternative i found is to change the the readfile function. There are some function alternative i think, combination between fopen and fpassthru worked on some server (it works on my private/offline server) but file_get_contents function works best.
this is the example code, (timthumb.php)
$fileSize = filesize( $cache_file ); header( "Content-Type: " . $mime_type ); header( "Accept-Ranges: bytes" ); header( "Last-Modified: " . gmdate( 'D, d M Y H:i:s', filemtime( $cache_file ) ) . " GMT" ); header( "Content-Length: " . $fileSize ); header( "Cache-Control: max-age=9999, must-revalidate" ); header( "Etag: " . md5($fileSize . $gmdate_mod) ); header( "Expires: " . gmdate( "D, d M Y H:i:s", time() + 9999 ) . "GMT" ); readfile( $cache_file ); exit;
i replace the readfile function and become
$fileSize = filesize( $cache_file ); header( "Content-Type: " . $mime_type ); header( "Accept-Ranges: bytes" ); header( "Last-Modified: " . gmdate( 'D, d M Y H:i:s', filemtime( $cache_file ) ) . " GMT" ); header( "Content-Length: " . $fileSize ); header( "Cache-Control: max-age=9999, must-revalidate" ); header( "Etag: " . md5($fileSize . $gmdate_mod) ); header( "Expires: " . gmdate( "D, d M Y H:i:s", time() + 9999 ) . "GMT" ); $img_data = file_get_contents( $cache_file ); echo $img_data; exit;
i hope this helps.





I use that file_get_contents function all the time, it save me a lot of time
Reply
jadi ingat bang ojak ngajar IBAD waktu tingkat 2..

Reply
Ojak Reply:
June 17th, 2009 at 5:51 am
aih… ngga enak si ganda,, buka kartu..
Reply
@ojak : kartu apa tuh bang? kartu tarot ya?
Reply
wah,makasi bang ojak…skrng timthumb ku nongol lagi

Reply
saved my day very good congratulations
Reply
tips di atas udah dicoba jg n filenya udah di chmod ke-777 tp koq tetep aza thumbnail di haidir.co.cc ngga bisa muncul ya?
.mohon pencerahannya bro..thanks bgt sebelumnya.
Reply
Brilliant, thanks for this! life saver
Reply
Thanks so much for sharing this, Ojak!!
Reply