Twitter: raymondcamden

Address: Lafayette, LA, USA

CFImage and paths with spaces

01-09-2013 3,341 views ColdFusion 8 Comments

A reader ran into an interesting issue:

I was working with CFIMAGE tag. Lets say

<cfset VARIABLES.myImage="">
<cfimage source="#VARIABLES.myImage#" name="VARIABLES.isLargeImage">

This works perfect, but when I have space in my image path like <cfset VARIABLES.myImage=" Folder/image.jpg"> It throws error. My path comes dynamically from DB in actual code. I also tried urlEncodedFormat on my path but that did not worked too. I think CF running on window server should be able enough to resolve paths which have spaces in its folder/file names.

To show you an example of what he said, I dropped an image with a space in it into a folder and then tried to cfimage it as he did:

When run, you get an error:

Interestingly enough, the browser has no issue with this. I've noticed this for years now - browsers (well at least Chrome) simply automatically escape the spaces for you.

If you try to fix it with URLEncodedFormat, it still fails. It is rather easy to see why if you output the result of the URL:


So to fix it, we need to urlEncode the file name. To do this we need to break apart the URL. There are multiple ways of doing this, but for simplicity sake, I used the parseURL UDF from It uses string functions to break apart a URL into its components. As an example, this code...

Returns these values:

Based on what I saw here, I thought this might be a nice way to rebuild the URL. I would not call this code 100% safe as it wouldn't pick up the username and password if they existed, but I wanted something simple.

And the result...

I hope this is helpful!


  • Dinesh Kanwar #
    Commented on 01-09-2013 at 9:16 AM
    Thank you Ray (I am that user who faced this problem). Solution makes sense to me. I will try this, I am sure this will work. But again, should not CF automatically resolve such path properly atleast when they are on window servers? As community, and the lovely language we work with, I was sure sooner or later I would have some solution. I am happy I have it sooner. More than that what I appreciate most that Ray finding time to address my problem. Not that I did not respected you earlier, but it has multiplied today for sure.
  • Commented on 01-09-2013 at 9:26 AM
    I think one could argue. I believe, stress believe, that since spaces are NOT valid in URLs, it should break. Chrome made it work because it was trying to be helpful. Basically saying, "Oh crap, a bad URL, I'll try to fix it for the user." But CF was being more strict.
  • Commented on 01-09-2013 at 11:43 AM
    Spaces should be avoided in file and folder names. They may have corrected this in newer version of exchange but sometimes people at my shop send me urls with spaces to files on our shared drive and exchange breaks the link. I end up having to copy the link and paste it into my browser ... not the end of the world, just a nuisance.
  • Commented on 01-09-2013 at 12:10 PM

    Is this particular use case, you should be able to safely just search the imageurl variable and replace any spaces with "%20". This would also resolve issues if there was a space in the relative path (although I'd hope that would never be the case.)

    <cfimage source="#replace(imageurl, ' ', '%20', 'all')#" name="isLargeImage">
  • Commented on 01-09-2013 at 8:56 PM
    @Dan: Much simpler, thanks. I knew there was an easier way
  • Johnny N #
    Commented on 01-10-2013 at 11:27 PM
    Interesting problem and this is an excellent example of why you shouldn't use spaces in folder or file names. While there is a solution you shouldn't have to create one for this. In my opinion it's poor development practice.

    I haven't worked in a shop where anyone used spaces, thank goodness! With that being said, I get the impression that most professionals don't follow Windows' sloppy standards when it comes to naming files/folders! 8.3 anyone?
  • Commented on 01-11-2013 at 5:44 AM
    Johnny, I think the point here is that users may create images w/ spaces in them. I know I've got a few I've downloaded. So it is less "Let's make em" and more "Let's handle em." One could also simply 'fix' images once uploaded. But don't forget, cfimage lets you work with remote images as well. You may not be able to avoid spaces.
  • papichulo #
    Commented on 01-15-2013 at 9:58 PM
    Sloppy Windows? You can have spaces in files and folders in OSX and Linux too. Hate Windows much? That's cool, I hate Macs.

Post Reply

Please refrain from posting large blocks of code as a comment. Use Pastebin or Gists instead. Text wrapped in asterisks (*) will be bold and text wrapped in underscores (_) will be italicized.

Leave this field empty