CFImage and paths with spaces

This post is more than 2 years old.

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!

Raymond Camden's Picture

About Raymond Camden

Raymond is a senior developer evangelist for Adobe. He focuses on document services, JavaScript, and enterprise cat demos. If you like this article, please consider visiting my Amazon Wishlist or donating via PayPal to show your support. You can even buy me a coffee!

Lafayette, LA

Archived Comments

Comment 1 by Dinesh Kanwar posted on 1/9/2013 at 8:16 PM

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.

Comment 2 by Raymond Camden posted on 1/9/2013 at 8:26 PM

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.

Comment 3 by Edward Rynearson posted on 1/9/2013 at 10:43 PM

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.

Comment 4 by Dan G. Switzer, II posted on 1/9/2013 at 11: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">

Comment 5 by Raymond Camden posted on 1/10/2013 at 7:56 AM

@Dan: Much simpler, thanks. I knew there was an easier way

Comment 6 by Johnny N posted on 1/11/2013 at 10:27 AM

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?

Comment 7 by Raymond Camden posted on 1/11/2013 at 4:44 PM

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.

Comment 8 by papichulo posted on 1/16/2013 at 8:58 AM

Sloppy Windows? You can have spaces in files and folders in OSX and Linux too. Hate Windows much? That's cool, I hate Macs.