Home > Shared Things > Fix disabled readfile function in PHP

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.

Share and Enjoy:
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • blogmarks
  • Reddit
  • StumbleUpon
  • Technorati
  • LinkedIn
  • Live
  • MySpace
  • Tumblr
  • Twitter

Shared Things , ,

  1. June 1st, 2009 at 16:47 | #1

    I use that file_get_contents function all the time, it save me a lot of time :em19:

    Reply

  2. June 17th, 2009 at 04:35 | #2

    jadi ingat bang ojak ngajar IBAD waktu tingkat 2.. :em56: :em46:

    Reply

    Ojak Reply:

    aih… ngga enak si ganda,, buka kartu..

    Reply

  3. June 18th, 2009 at 06:26 | #3

    @ojak : kartu apa tuh bang? kartu tarot ya?

    Reply

  4. July 10th, 2009 at 10:58 | #4

    wah,makasi bang ojak…skrng timthumb ku nongol lagi :em70: :em02:

    Reply

  5. silva
    May 18th, 2010 at 22:06 | #5

    saved my day very good congratulations

    Reply

  6. July 8th, 2010 at 13:56 | #6

    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? :em39: .mohon pencerahannya bro..thanks bgt sebelumnya.

    Reply

  7. July 23rd, 2010 at 11:29 | #7

    Brilliant, thanks for this! life saver

    Reply

  8. Paul
    July 26th, 2010 at 20:05 | #8

    Thanks so much for sharing this, Ojak!!

    Reply

  1. June 12th, 2009 at 00:31 | #1
  2. February 6th, 2010 at 01:45 | #2
  • :em39:
  • :em66:
  • :em16:
  • :em18:
  • :em43:
  • :em68:
  • :em04:
  • :em63:
  • :em70:
  • :em21:
  • :em25:
  • :em12:
  • :em19:
  • :em57:
  • :em53:
  • :em42:
  • :em22:
  • :em40:
  • :em11:
  • :em33:
  • :em24:
  • :em69:
  • :em62:
  • :em27:
  • :em14:
  • :em32:
  • :em09:
  • :em17:
  • :em36:
  • :em64:
  • :em56:
  • :em55:
  • :em51:
  • :em65:
  • :em31:
  • :em72:
  • :em23:
  • :em38:
  • :em20:
  • :em34:
  • :em48:
  • :em29:
  • :em07:
  • :em52:
  • :em58:
  • :em41:
  • :em13:
  • :em02:
  • :em49:
  • :em26:
  • :em30:
  • :em10:
  • :em67:
  • :em03:
  • :em35:
  • :em59:
  • :em46:
  • :em08:
  • :em28:
  • :em60:
  • :em37:
  • :em71:
  • :em15:
  • :em50:
  • :em61:
  • :em01:
  • :em06:
  • :em44:
  • :em47:
  • :em54:
  • :em45:
  • :em05: