Link to home
Start Free TrialLog in
Avatar of WestCoast_BC
WestCoast_BCFlag for Canada

asked on

How do I use imagenew if the file name contains spaces

I am trying to use the Codlfusion function imageNew. It works for me if the filename does not contain any spaces. If it does I get an error. For example:
<cfset thisImageside=imageNew("/path_to_file/filename.jpg")>

Open in new window

works but
<cfset thisImageside=imageNew("/path_to_file/file name with space.jpg")>

Open in new window

gives me an error, I get an Access denied error. I have tried using urlEncodeFormat for the file name but then I get the error that the file does not exist.

If anyone can shed some light regarding this problem I would very much appreciate it.
Avatar of David H.H.Lee
David H.H.Lee
Flag of Malaysia image

Try this:
<cfset imageurl = "http://17.0.0.1/path_to_file/file name with space.jpg">
<cfset parts = parseURL(imageurl)>
<cfdump var="#parts#">

Open in new window

parseURL: https://cflib.org/udf/parseUrl
Avatar of WestCoast_BC

ASKER

If I use parseURL I get the error:
No matching function [PARSEURL] found

Open in new window

I do not need to use parseURL because I already have the filename without the path. It looks to me that the example provided comes from: https://www.raymondcamden.com/2013/01/09/CFImage-and-paths-with-spaces

I tried using urlEncodedFormat as suggested in Raymond Camden's article but I get the error that the file does not exist
parseURL() is the custom function as shown in previous link.

Try another approach:
<cfset newurl = parts.scheme & "://" & parts.domain & parts.directory & urlEncodedFormat(parts.file)>
<cfoutput><p>#newurl#<p></cfoutput>
<cfimage source="#newurl#" name="yourImage">

Open in new window

I have already tried what you suggest but I get the error that the file doesn't exist. Here is my error message:

file or directory [my_path]/images/2%20j%20live%20Ottawa07%2Ejpg not exist
 
Is this file path exist as given accessible in browser?
[my_path]/images/2%20j%20live%20Ottawa07%2Ejpg

Open in new window

No, it is a path in my server starting with c:/home/ .....
Can you refer the hosted file from public URL domain? otherwise, you will get permission access error.
Furthermore, you can use double quotes ("") to refer the file name with spaces.

Try this:
<cfset thisImageside=imageNew(""http://yourdomain.com/path_to_file/file name with space.jpg"")>

Open in new window

If I use the same code but with a filename that does not contain any spaces then it works fine. In both cases the path to the file is exactly the same.

Double quotes causes an error, the page does not run. Given that my test works files that do not contain a space I am confident that my path is correct.
how about replace space with %20 if your existing path is working fine? %2E is dot replacement after encoded.

<cfset thisImageside=imageNew("/path_to_file/file%20name%20with%20space%2Ejpg")>

Open in new window

I have tried that. Here is my error message:
file or directory [my_path]/images/2%20j%20live%20Ottawa07%2Ejpg not exist

Open in new window


To make sure that the file exists I added
<img src="[my_path]/images/2%20j%20live%20Ottawa07%2Ejpg" width="300px">

Open in new window

and the image loads fine
I do not use CF and can't provide the exact answer, but I believe your issue is your file name with spaces has to contain quotes.  https://docs.microsoft.com/en-us/troubleshoot/windows-server/deployment/filenames-with-spaces-require-quotation-mark

In any of my web apps, I just don't allow the use of spaces in file names. If the image is being uploaded, I just do a string replace to switch out the space for an underscore.
ASKER CERTIFIED SOLUTION
Avatar of WestCoast_BC
WestCoast_BC
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial