06 Jan 2012

How to Fix Timthumb using a Virtual Directory (URL contains tildes (~))

General, PHP, Wordpress 36 Comments

The timthumb tilde issue has been discussed for almost a year now on the official timthumb site, and the developers have yet to do anything to provide a fix for users in development environments.

The issue is that when developers are developing sites using a URL structure such as:, timthumb does not correctly parse the folder structure and returns a broken image. In this case, a proper path might be: /home/mysite/public_html/images/dog.jpg, however timthumb creates the broken path: /home/mysite/public_html/~mysite/images/dog.jpg.

For the record, I believe timthumb is a horrible idea and should be avoided at all costs (security and performance issues). However there are many instances where you might purchase a template to find it is completely integrated with timthumb, and the amount of time it could take to remove timthumb from the template is not worth it.

How to fix

Make sure you are using the latest version of timthumb. At the moment of this writing, the version is 2.8.5

At line 209 you will find the code:

$this->src = $this->param('src');

Replace that with:

//check if tilde is found in src
   $url_parts = explode('/',$this->param('src'));

   foreach($url_parts as $url_part)
      //do not include any part with a ~ when building new url
         $new_dev_url .= $url_part.'/';

   //remove trailing slash
   $new_dev_url = substr($new_dev_url,0,-1);

   $this->src = $new_dev_url;
   $this->src = $this->param('src');

This isn’t the most elegant solution, however the only time you should be using it is while you are developing and debugging the site (tildes should never be in the URL of a live website). Once you go live, it will automatically skip over that extra processing and work just as timthumb is intended.

Additional fix for $_SERVER[‘DOCUMENT_ROOT’]

There might be a chance that timthumb is still not working, and that could be because $_SERVER[‘DOCUMENT_ROOT’] is not being properly defined. To get around this, we need to manually define $_SERVER[‘DOCUMENT_ROOT’] at the beginning of the document:

Above this line (line 23):

define ('VERSION', '2.8.5');

Insert the root path of your website, something like this:

$_SERVER['DOCUMENT_ROOT'] = '/home/mysite/public_html/';

I hope that helps, and if it is still not working for you, I highly recommend visiting the official timthumb website and talk with the developers.

36 Responses to “How to Fix Timthumb using a Virtual Directory (URL contains tildes (~))”

  1. Paul says:

    Thanks so much for this. Worked perfectly for bluehost test site!

  2. Nicklas says:

    Actually, it worked. Thanks so much!!

  3. Pete says:

    Excellent solution that I’ve needed for a while, many thanks!

  4. gamaro says:

    Thanks a lot for this.
    Anyone can help me to determine my document_root? I’m a little confused, the thumb.php file is in this directory: /public_html/wp-content/themes/apex/functions

    What should I put in $_SERVER[‘DOCUMENT_ROOT’] = ‘/home/mysite/public_html/'; ?

    Many thank,


  5. chad says:


    This article should help point you in the right direction: http://www.helicron.net/php/

  6. gamaro says:

    Thanks Chad for your quick reply

    Kind Regards


  7. Yop says:

    Thanks a lot,
    it worked!

  8. Dalken says:

    Wahou !! THANKS SOOOO MUCH !!
    You saved my day ! :)

  9. mzkymz says:

    After reading through all the threads related to this problem and not finding a solution this has been such a BIG help. Can’t thank you enough for sharing!

  10. Shams says:

    Ultimate solution bro.. Really great work. I had searched a lot regarding server path issue but found nothing. You done a great job. Ohh God ! How worried I was.

    Server path may be a weird issue for timthumb and you provide an awesome and easy to use solution.


  11. Antonie says:

    Thank you so much, it works great.
    I’ve Googled long and hard to find a Timthumb solution for temporary hosting URLs.
    This was the only tutorial which worked to fix the problem.

  12. Lyd says:

    Thank you!!!! Even a wordpress newbie like me was able to follow these instructions and make it work!

  13. Corinne says:

    Thanks so much for this information; I was so excited when I found it and was hoping it would fix my problem. I just tried it and sadly I am still having a problem on my test site, . I’ll head on over to the timthumb site and see if I they have a solution.

  14. Matthew says:

    Hey there,

    I tried this as well and I was still having issues. Could it be that it’s because TimThumb is now 2.8.10 now?

    Or maybe it’s because i did the document root wrong? My site is , so for that I put:
    $_SERVER[‘DOCUMENT_ROOT’] = ‘’ – is that correct?

  15. chad says:


    Your document root is incorrect, it would be something like /home/kieepcon/public_html/

  16. solaceten says:

    Yes, I too have tried this but it is no longer working with V 2.8.10

    I also came across this post but not exactly sure what they suggest to do witht the code (put it in timthub.php?)

    Anyway, I tired it but no good either !

    Still searching for a solution

  17. solaceten says:

    Ahh – and here is the link to the mentioned site:


  18. mick says:

    Doesnt work for me, tried it all from this forum and all other forums point to this one.

    Pretty ordinary I think…..

  19. coisty says:

    I wish I had found this page a lot sooner because I’ve been banging my head all day trying to get timthumb to work on my dev server.

    Thank you soooooo much!

  20. Scott says:

    Thank you for posting this solution. The problem had to do with a tilde in the filepath.

    Timthumb is such a hassle and I try to avoid it but unfortunately there are a lot of old sites out there using it.

  21. Jason says:

    This has made my day! Thank you for posting this solution.

  22. Firebrand Designs says:

    Dude, I’ve have been searching for a day and a half for a way to get this script working in the theme we purchased for a client, and this did it!!! The servers we develop on use the url convention. Thanks for this post! You rock!

  23. Shahin says:

    Well, the only solution that worked for me! Many thanks!

  24. Ed Booth says:

    Thank You! Thank You! Thank You!

    It’s so nice to google something and find a solution that works. You saved me hours of tearing apart timthumb to figure out how to work around this.

  25. Dennis Gal says:

    I love you! I seriously do… 8 hours of agony and pain… 2 hours of Live chat with HostGator regarding permissions and white listing security_mod and it all came down to this!

    THANK YOU! I’ve sent a small donations as a token of my gratitude.

  26. Marcelo says:

    Works very fine in Just host!

    Thanks !

  27. Metrov says:

    I feel pretty dumb, but why am I the only one who doesn’t know what file we are talking about and where to find it. I can easily change the code as instructed, but what is the file name? And where do I find it? Is it “thub.php” as someone mentioned? I can’t find that anywhere on my server. Thanks for the help.

  28. chad says:

    @Metrov, unfortunately there is no easy to way to help, as it can be in any folder and the file name can be modified. If in doubt, a way you can find the file is to download the site to your local machine, then run a tool like grepWin (http://stefanstools.sourceforge.net/grepWin.html) against that site directory. You can search for “timthumb”, which should then show you the file, and where it is located.

    I hope that helps!

  29. Matt says:

    For anyone struggling with the same problem, this edit worked for me in timthumb.php (above line 23):

    define(‘LOCAL_FILE_BASE_DIRECTORY’, “/home/mysite/public_html/”);

    INSTEAD of:

    $_SERVER[‘DOCUMENT_ROOT’] = ‘/home/mysite/public_html/';

  30. Joshua says:

    @Matt, that solution didn’t seem to work… any other ideas :S?

    I’m using

    define(‘LOCAL_FILE_BASE_DIRECTORY’, “/usr/local/apache/htdocs”);

  31. Howard says:

    None of these solutions worked for me. I’m using a Bluehost temp site until I move the pointers, so it has a ~earthpl as the site name….. Still stuck . Any other suggestions welcomed.

    My site uses timthumb Version 2.8.10

  32. Spirit says:

    thank you very much it works fine my site uses timthumb version 2.8.11 and this method works like a charm.

  33. Avinash says:

    I read above comments, got that problem is solved .
    But i am unable to open given url http://site-spring.com/tim-thumb-fix-temporary-url/#sthash.yU0bH5EA.dpuf for solution.
    Please provide me solution or new url.

  34. Jamie says:

    Thank you, solved my problem.

  35. husen says:

    how to define $_SERVER[‘DOCUMENT_ROOT’] for local machine? i have installed wordpress on lamp

  36. lanhub on "[Plugin: Rio Portfolio] Image never gets loaded" | Wordpress Problems & Erros - How to fix says:

    […] nvm…found the problem (the filename)http://elementdesignllc.com/2012/01/how-to-fix-timthumb-using-a-virtual-directory-url-contains-tilde… […]

Leave a Reply