Friday, March 12, 2010

imagecache wrong path – SOLVED!

This is targeted specifically for Windows and IIS 6.0 or 7.0, but it works for Apache to.

Probably the first instant problem everyone will experience is this:

The selected file xxx.JPG could not be uploaded. The file is not a known image format.

It doesn’t actually matter what file you try to upload if you’re using IIS or Apache you will get this error. The fix is to edit the file in

/includes/image.inc, and change line 117 from:

if (!is_file($file)) {

to:

if (!is_file($file) && !is_uploaded_file($file)) {

I recon we all think Drupal is great, but I wasn’t happy when it took me three days to solve the imagecache problem. If you’re using IIS and you’ve done everything you can think of you could skip to the end, but this post might make a useful check list to see that you’ve done everything necessary to get imagecache working.

The problem I’m having is that when I upload an image to include in a node I get the following:

* A thumbnail of my image is created and stored in the directory sites/default/files/imagefield_thumbs – which to me means that the imagefile module is working ok.

* An unadjusted copy of my original .jpg file is uploaded to the directory sites/default/files

imagefile module appears to be working fine

imagefile module appears to be working fine

I can see that the directory sites/default/files/imagecache has been created, however the contents of this directory are completely empty of both files and folders – so it would seem from this apart from creating a directory the imagecache module isn’t really doing a lot.

When I browse my homepage I can see the node being displayed (and if I use IE I can see an empty image container) which shows that there is code in there to display an image however it’s pointing to a directory of a path that doesn’t exist and to an image that isn’t there:

sites/default/files/imagecache/fullsize/IMG_0119.JPG

IE shows an empty image containet

IE shows an empty image container

The fix

Firstly as of this post 24/09/09 I’m using the very latest versions of these modules – they are:

Drupal 6.14
imageapi-6.x-1.6.tar.gz
imagecache-6.x-2.0-beta10.tar.gz
imagefield-6.x-3.1.tar.gz

It is my aim that by the end of this post I will have found a way around this problem by getting to the bottom of what’s going on and I hope that it will work for you to:

BAM!

Working fine with unadjusted image (ie no image operations assigned)

Working fine with unadjusted image (ie no image operations assigned)

Ok, the first thing I tried was to remove the selection of any filter type. This means rather that select my own created preset filter type I called ‘fullsize’ and ‘thumbnail’ which have filter actions applied to them, I simply selected Image from the drop down list and not any of my own created stuff like ‘fullsize’ or ‘thumbnail’ which have image operations assigned to it. This means that the HTML now points to:  /sites/default/files/IMG_0119.JPG

Which is fair enough, but then it means the HTML is now pointing to an image that I know is there because of imagefile and not because imagecache is now working.

My next step is to create a new preset filter type for imagecache arbitrarily called ‘test’ – this should create a directory called test that has the path /sites/default/files/imagecache/test where I assume all images created by the imagecache module with the filter called ‘test’ are stored.

Ok – Bingo!

I created a new preset filter type ‘test’ I didn’t apply any actions to it, but the directory ‘test’ was not created. Furthermore there is a link to Preview the non-existant directory with the url:

/sites/default/files/imagecache/test/imagecache_sample.png?1253793901

Ok, so ‘test’ is not created but I do see an image called ‘imagecache_sample.png’ which is good but it’s in my file upload root directory: sites/default/files/imagecache_sample.png not sites/default/files/imagecache/test/imagecache_sample.png

To check this further I’m going to delete this sample image and then try my test again just to make sure exactly when the imagecache_sample.png file is created.

Yup. The sample image file is created as soon as I click on the ‘Save Preset’ button – but why does it not create the correct directory imagecache/test and save it there? Could it be a folder permissions thing and just not throwing up an error?

Maybe – I’ll just go and confirm that to.

Ok. My files folder has read and write permissions set but NOT ‘modify’ I’ve just added that permission, I’ll run the test again creating a new preset filter type called ‘test2′ – again with no actions applied to it.

No. Yet again there is no new directory created called test2, but again, the imagecache_sample.png is written to sites/default/files.

I’ve just gone and updated my version of PHP from 5.2.8 to 5.2.11 – Still doesn’t help. I’ve also created a tmp directory in my Drupal 6.14 root which is fully writable – still doesn’t work.

I’ve just noticed that after running Cron I can see that GD Library is listed twice! I don’t know if this is important or not but at this stage I’m treating absolutely everything as suspicious and seeing that GD library is used by imagecache to create the images I’m going to now put GD library under the microscope.

Okay, having just disabled the GD libraries, downloaded ImageMagick for the Win32 platform from here I’m still getting the same problems – no fullsize image being created, the thumbnail (which seems to be created by imagefile in imagefile_thumb) works fine – but then that’s imagefile and NOT imagecache – so yet again it seems that after even more bleeding out the eyeballs hasn’t paid off.

I’ve spent days on this, I visited just about every website on Earth, I’ve updated my PHP, path directories, modules. I’ve hacked away and done my best to find a solution and just at the moment I must admit to being at a complete loss.

Right I’m back again!

It’s been another entire day on this and after a bit of encouragement from the comment section I’m sticking with this problem until it’s solved. Sorry that this is turning out to be such a long post but it’ll be worth it when imagecache is working!

Firstly YES I have read the troubleshooting guide which you can find here: To save you looking I’ve copied all the salient points here (with a bit of my added commentary) This is what the author of ImageCache has to say: (my comments are in bold italics)

What can go wrong?

No images in the imagecache directory

First, don’t jump the gun and think there is something wrong. Imagecache only creates its images on demand. This means that when you create an imagecache profile, the imagecache directory {}/files/imagecache/{profilename}/ starts off empty.

(No it doesn’t it starts off not existing, it never gets created – ever and nothing ever starts working either, my directories are completely writable as well.)

Files directory

Go to Administer > Site configuration > File system. Are the file system path and temporary directory writable by Drupal? Is the download method public?

(Done that, checked it, double checked it and absolutely confirmed and yes they have ‘method public’)

Rewrite

Obviously rewriting needs to work. And clean URL’s. Does rewriting work in your files directory? Is .htaccess allowed?

(I’m running Drupal on my own Windows 2003 Server with IIS 6.0 as the web server. I am also using the full version of Helicon’s ISAPI_Rewrite 3.)

Try this:

  1. Upload a file. After that http://www.example.com/files/file.jpg should return the file. And http://www.example.com/files/nonexistantfile.jpg should present a Drupal “page not found” error.
  2. If you get an error from Apache instead of Drupal, rewriting is not working. Check (with your host) whether .htaccess is allowed.
  3. If not then this part from the Drupal .htaccess file should be copied to the Apache configuration file:
    RewriteEngine on
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]

  4. If .htaccess is allowed make sure the original Drupal .htaccess file, including the exact lines above, exists in the main Drupal directory. And secondly make sure the .htaccess file under your files directory contains exactly these two lines (no more no less):
    SetHandler Drupal_Security_Do_Not_Remove_See_SA_2006_006
    Options +FollowSymlinks

    Note for sites upgraded from Drupal 4.7: Older versions of Drupal wrote an extra line to the .htaccess file disabling mod_rewrite which is no longer necessary but will interfere with ImageCache.Of course both files should be readable by Apache.

  5. If it still doesn’t work, and you are using Drupal in a subdirectory, uncomment and modify the RewriteBase.
    RewriteBase /<your subdirectory here>

Hummm. The bit above looks interesting… I’m using IIS 6.0 on a Windws 2003 Server. The .htaccess file is only for Apache Servers. It looks as though I’m definately onto something here. I just did a google for ‘imagecache ISAPI_rewrite’ and I found a post – ONE post in the World that says something useful. If you are using IIS 6.0 as your web server you will want to take a look at this site here:

http://www.helicontech.com/forum/forum_posts-TID-9726.htm

Again, for my own clarification and ease of access I have included the salient points; ‘zeman’ on the helicontech website has this to say…

“Editing the nested .htaccess in “sites/default/files” and added the default rules worked for me as well but I had to change the final rule to accommodate the new location of .htaccess. Using Rewrite 3.”

RewriteRule ^(.*)$ /index.php?q=sites/default/files/$1 [L,QSA]

[It] Might be worth adding this to the Drupal instructions page.” (No kidding!)

What zeman is saying above here is that Drupal’s original .htaccess file should be added to the contents of the .htaccess file in the location sites/default/files -  also including his modification “to accommodate the new location of .htaccess”

So what this means now is that the .htaccess file in sites/default/files should look like this:

SetHandler Drupal_Security_Do_Not_Remove_See_SA_2006_006
Options +FollowSymLinks

#This is the code copied from the .htaccess in Drupal’s root directory tacked on in here.

RewriteEngine onRewriteCond %{REQUEST_FILENAME} !-fRewriteCond %{REQUEST_FILENAME} !-dRewriteCond %{REQUEST_URI} !=/favicon.ico

#The path below is no longer valid because this .htaccess file is in sites/default/files#RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]#So we simply change it to include the path to our images folder

RewriteRule ^(.*)$ /index.php?q=sites/default/files/$1 [L,QSA]

If you are using Ionic’s IIRF use this code in your IsapiRewrite4.ini file

RewriteCond %{HTTP_HOST} ^(caspianit\.co\.uk).*$ [I]RedirectRule ^/(.*)$ http://www.caspianit.co.uk/$1 [I,R=301]RewriteCond %{REQUEST_FILENAME} !-fRewriteCond %{REQUEST_FILENAME} !-dRewriteRule ^/(.*)$ /index.php/$1 [L]

This is the most promising snippet of code I have seen in days. I’m going to reboot my server just to be on the Uber safe side make a cup of tea and try it out. Fingers crossed…

IT WORKS!! YESSSSS!!! HAHAHAHAHAHAHAHAH……

Three days of bleeding out the eyeballs and I’ve finally got it working! A huge thank you to ‘zeman’ and everyone who has provided insights on this problem. Quite frankly I’m amazed there aren’t thousands of people with this issue. It’s probably because most people are using Apache and not IIS. Well it may be 2:05am but I think I’m probably to excited to sleep now.

Just a quick footnote if yours is still not working just remember to create a new node and upload your image first – before trying to access it with http://yourdomain.com/files/image.jpg just go to your homepage normally and get imagecache to create the file first!

*Update*Just for completeness, I have written a full tutorial / walk through for installing IIRF on Windows 2003 Server. There’s everything you could need there from start to finish, it’ll only take you a few minutes to go through – it’s a recommended read! http://www.caspianit.co.uk/iirf-drupal-and-iis6/

All the best

Sci-Fi Si.

Related posts:

  1. GD library is listed twice. Drupal 6.14
  2. Site off-line The site is currently not available due to technical problems. Please try again later. Thank you for your understanding. (SOLVED!)
  3. Drupal – The selected file… could not be uploaded. Only JPEG, PNG and GIF images are allowed
  4. IIS and Drupal – Enable Clean URL’s
  5. Drupal Gmail Module – Solved.


22 Comments

  1. Comments  Brad   |  Monday, 28 September 2009 at 12:11 am

    Hey man… Hurry up and get this damn thing working!!!

    I’ve been trying for bloody ages and it just doesn’t want to play. No matter what I change the paths to it doesn’t work; in fact, if I change the upload paths to where Imagecache says the thumbnails are, imagechace chan’ges it’s path to include the former changes!!!

    Just like you, Imagecache doens’t seem to make any folders no matter what permissions are in place…

    I hope you have more luck than me :(

    B

  2. Comments  Simon Nicol   |  Tuesday, 29 September 2009 at 2:13 am

    Hey Brad!

    I’ve just updated this post and I finally got it working at 2:05 this morning! You won’t believe the aggravation imagecache has caused. Are you hosting Drupal on IIS? I am and if you are to I know what the problem is – come back and have a look.

    All the best

    Simon

  3. Comments  IIRF Drupal and IIS6 | Computer Services - Croydon Crawley & Brighton.   |  Tuesday, 06 October 2009 at 5:40 pm

    [...] The reason ISAPI_Rewrite fails to work on IIS when trying to get imagecache working is because of the .htaccess file in the sites/default directory. I have a complete post about it here. [...]

  4. Comments  Brad Ainsworth   |  Thursday, 22 October 2009 at 7:38 pm

    Tried your suggestions re. the .htaccess files and copying the root file’s content into the sites/default/files/ .htaccess… Didn’t work straight away, so I left it for about a week for my host’s servers to reboot and hey-presto…. WORKING!! Host is using an ‘enhanced’ Apache server, just to let you know… :)

    Sooooo happy that it’s working now and I can’t thank you enough!!

  5. Comments  Google AJAX Translation not working - WordPress   |  Thursday, 22 October 2009 at 7:51 pm

    [...] getting there. Have a quick look at one of my posts here in the comments section below you will see the [Translate] function working perfectly! This is with [...]

  6. Comments  Stumped   |  Friday, 30 October 2009 at 10:57 pm

    Nice work! Thanks for providing an in-depth tutorial. I felt sure it would work for me.

    And yet, it doesn’t. Drupal 6.14, IIS5.1 and IIS6 (trying in two different installations), latest modules, Ionic IIRF.

    I’ve got the sites/default/files .htaccess file looking exactly like yours. I took the DrupalRules.ini example file that came with IIRF, renamed it IsapiRewrite4.ini. And . . . nothing.

    Here’s the thing: I can force it to create the thumbnails by setting file system to “private” downloads. Once that’s done, it works fine. I just don’t want to have to keep switching the file system every time there’s new content.

    Any idea what I’m missing?

  7. Comments  Simon Nicol   |  Saturday, 31 October 2009 at 12:42 am

    @Stumped

    I found the default DrupalRules.ini file didn’t work for me either, but the following code is the IIRF code that’s running for manonmars.co.uk (my Drupal site I keep blogging about here) right now on my 2003 Server, try this as your IsapiRewrite4.ini file (obviously you need to put your own domain name in – lol)

    RewriteCond %{HTTP_HOST} ^(www\.manonmars\.co\.uk).*$ [I]
    RedirectRule ^/(.*)$ http://manonmars.co.uk/$1 [I,R=301]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^/(?!favicon.ico$)([^?]*)(?:\?(.*))?$ /index.php?q=$1&$2 [L]

    You might also want to make sure that the folder in sites/default/files has the computer user IUSR_ComputerName with full write/modify rights.

  8. Comments  Stumped   |  Saturday, 31 October 2009 at 10:06 pm

    Simon, you’re a genius. That did the trick.

    I never reached the point where blood was literally trickling from my eyeballs, but I felt I was getting close.

    Thank you, thank you, thank you!

  9. Comments  jordi   |  Tuesday, 03 November 2009 at 8:04 pm

    Hi

    Thanks a lot. You solved the problem.

    For me, the key was to create an .htaccess file inide the sites/default/files folder with this content

    SetHandler Drupal_Security_Do_Not_Remove_See_SA_2006_006
    Options +FollowSymlinks

    Thanks a lot.

    jordi

  10. Comments  Bob   |  Thursday, 05 November 2009 at 11:58 pm

    Guys,

    I have clean URL’s enabled, IIS 6.0, Drupal 6.13 and uber 2.0. I set the .htaccess file in sites/default/files and deleted the line that said Options None. Put this code into the .htaccess (RewriteRule ^(.*)$ /index.php?q=sites/default/files/$1 [L,QSA]) located at /sites/default/files and it still isn’t working. At a loss.

  11. Comments  Simon Nicol   |  Friday, 06 November 2009 at 12:17 am

    @Bob

    Hey there, thanks for the visit. Clean URL’s is good but it does make a difference if you’re using ISAPI_Rewrite3 or IIRF etc..

    If you’re using ISAPI_Rewrite you would want to use this full code:

    SetHandler Drupal_Security_Do_Not_Remove_See_SA_2006_006
    Options +FollowSymLinks

    RewriteEngine on
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_URI} !=/favicon.ico

    #The path below is no longer valid because this .htaccess file is in sites/default/files
    #RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]

    RewriteRule ^(.*)$ /index.php?q=sites/default/files/$1 [L,QSA]

    If you’re using IIRF then the code posted above @Stumped should do the trick. Do you have IUSR_ComputerName with modify permissions granted? Has your server re-booted since you have made these changes? PHP will need to restart…

    Let me know how it goes, the more info you can provide the better…

    All the best

    Sci-Fi Si

  12. Comments  Bob   |  Friday, 06 November 2009 at 4:15 pm

    Thanks for the reponse, I didn’t reboot after making the changes. You know what, I am not sure which I am using as far as the rewrite. How would I check this out, I could’ve swarn I used the ISAPI_Rewrite, which is what I needed to get clean urls to work. If you know a way to check let me know, if not I can dig on this when I get on my server.

    Thanks for the help!

  13. Comments  Bob   |  Friday, 06 November 2009 at 5:35 pm

    I am using Isapi_rewrite, I remember now, using their lite free version. I think I am getting confused on where the code goes in which .htaccess file. Here is what I have. At the root of drupal I have added code to .ht so it now looks like this:

    RewriteEngine on
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ /index.php?q=sites/default/files/$1 [L,QSA]

    The .htaccess in /sites/default/files, it looks like this:

    SetHandler Drupal_Security_Do_Not_Remove_See_SA_2006_006
    Options +FollowSymlinks

    Is this correct or am I putting the code in wrong. Tried creating a preset and it doesn’t create a folder in the imagecache folder.

  14. Comments  Bob   |  Friday, 06 November 2009 at 5:51 pm

    Also, tried just adding this code to htaccess at sites/default/files/ and leaving the htaccess alone at drupal root, still nothing.

    SetHandler Drupal_Security_Do_Not_Remove_See_SA_2006_006
    Options +FollowSymLinks
    RewriteEngine on
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_URI} !=/favicon.ico
    RewriteRule ^(.*)$ /index.php?q=sites/default/files/$1 [L,QSA]

  15. Comments  Bob   |  Saturday, 07 November 2009 at 6:07 am

    Crap, could this be because I am using the lite version of isapi_rewrite? On the website for the lite version it says:

    # No support for distributed .htaccess configurations, directory and web site level configurations. Only one global httpd.conf configuration file will be loaded from Lite version installation folder.

  16. Comments  Bob   |  Saturday, 07 November 2009 at 6:57 am

    Hmmmmmm…Got it to work by switching to Private instead of Public. I have no idea what the drawbacks are, anyone know?

  17. Comments  Simon Nicol   |  Saturday, 07 November 2009 at 12:58 pm

    @Bob

    I really didn’t have any luck with the Lite version of ISAPI_Rewrite at all , as soon as I switched to the Full version – boom! Everything started working straight away…

    ..the trouble is that it costs $90, so in the end I went with IIRF – which is completely free and and has more functionality! I even had it’s author Ionic Shade leave a post on this site! I highly recommend giving it a try, it also means you’ll be able to have a different configuration for each website – I’m running Drupal and WordPress, ColdFusion and PHP on this server and it works a treat

    :)

    To find out which URL rewriter you are using (if you’re running IIS as your web server) just have a look at the ISAPI Filters tab in the properties of your website, there will be a ‘Filter Name’, you can also check the filename of the executable .dll that’s doing the re-writing for you.

    Also you do not need to change the .htaccess file in Drupal root, just the one in sites/default/files

    *Is this correct or am I putting the code in wrong. Tried creating a preset and it doesn’t create a folder in the imagecache folder.*

    It won’t until you’ve not only uploaded your image but also visited the page that the ‘resized’ image should be on – it will only manipulate the image once the page has been requested

    Not sure what the Public/Private drawback might be – maybe – It limits other users from being able to upload an image??

  18. Comments  Chris   |  Wednesday, 09 December 2009 at 3:08 am

    Simon Nicol —

    Up top, my man! Thanks for the great help, I would have hated to have to figure that out on my own.

    For anyone curious:

    I got imagecache working on IIS 5, using the FULL VERSION of ISAPI Rewrite 3, using the .htaccess directives suggested by zeman for the sites/default/files directory.

    I was UNABLE to get imagecache working with the FREE VERSION of ISAPI Rewrite, and lost patience trying to configure IIRF, which I could not seem to configure on this particular XP system properly. This will be worth the $100 license fee to my client, I think, since they have a particular reason for running IIS (suffice it to say, their accounting software offers a web-based interface).

    Kudos,
    Chris

  19. Comments  Simon Nicol   |  Wednesday, 09 December 2009 at 3:38 am

    Nice one Chris, thanks for the comment and I’m glad I could help! There’s code in one of the comment boxes above that will get IIRF running for you. It saves a $100. I’ve written a full post on how to install it here.

    All the best

    Simon

  20. Comments  Jorge   |  Monday, 01 March 2010 at 3:48 pm

    Hi Simon

    I’m trying to migrate a site from unix to IIS7. This site is using drupal and the imagecache is heavily used. However I’m having trouble getting it working.

    The site is http://esn.upv.es

    Rewriting is working cause I have activated the nice urls but….

    This works:

    http://esn.upv.es/sites/default/files/catadevinos.jpg

    This doesn’t:

    http://esn.upv.es/sites/default/files/imagecache/header/catadevinos.jpg

    In the original site, both are working

    http://esnupv.org/sites/default/files/catadevinos.jpg
    http://esnupv.org/sites/default/files/imagecache/header/catadevinos.jpg

    Any idea what can be wrong??

    Thank you!

  21. Comments  Simon Nicol   |  Monday, 01 March 2010 at 10:27 pm

    Try this:

    Hey Jorge, I’m with you.. I’ve found IIS7 really very different from IIS6. 7 is a lot more like Unix than 6 ever was. You will find yourself dealing far more with scripts now than you ever did with IIS6. It’s not a bad thing tho’ IIS7 just takes a little bit of adapting to that’s all.

    From what I can guess, you’re only problem will be with what your unix .htaccess file is doing – I take it that your web server is Apache?

    I may speek a little bit of Spanish but the first thing I noticed your server displaying (guessing 2008 with IIS7?) is this:

    Error del servidor en aplicación “ESN.WEBS.UPV.ES” – Look at the URL! it’s adding WEBS in between the address you need.

    The rest of this stuff actually looks fine to me and there’s no reason it shouldn’t work – fix the above problem and all of these other things should go away -

    http://esn.upv.es/sites/default/files/imagecache/header/catadevinos.jpg

    http://esn.upv.es/sites/default/files/catadevinos.jpg – working!

    Dirección URL solicitada http://esn.upv.es:80/index.php?q=sites/default/files/imagecache/header/catadevinos.jpg
    Ruta de acceso física E:\vhosts\esn.webs.upv.es\httpdocs\index.php
    Método de inicio de sesión Anónimo

    All the best

    Simon

  22. Comments  Jorge   |  Tuesday, 02 March 2010 at 8:56 am

    Thanks for your quick answer Simon.

    esnupv.org is UNIX with apache
    esn.upv.es is WINDOWS 2008 with IIS7

    I’m trying to move from A to B :-)

    esn.upv.es is an alias of esn.webs.upv.es so that shouldn’t be a problem. In fact this URL is working (note the ‘webs’ in the domain):

    http://esn.webs.upv.es/sites/default/files/catadevinos.jpg

    It seems to me that the petition is never reaching the imagecache module…. Is that possible? How can I check it?

    Thanks again for your help!

Leave a Reply