开发者

Pound sign (#) in filename causing error

I have a very simple f开发者_开发问答ile upload that allows users to upload PDF files. On another page I then reference those files through an anchor tag. However, it seems that when a user upload a file that contains the pound sign (#) it breaks the anchor tag. It doesn't cause any type of Coldfusion error, it just can't find the file. If I remove the #, it works just fine. I am sure there are a number of other characters that would have this same issue.

I've tried putting URLEncodedFormat() around the file name inside the anchor but that doesn't help. The only other thing I could think of was to rename the file each time it was uploaded and remove the "#" character (and any other "bad" character).

There has got to be an easier solution. Any ideas?


If you control the file upload code try validating the string with

 IsValid("url",usersFileName) or
 IsValid("regex",usersFileName,"[a-zA-Z0-9]")

Otherwise if you are comfortable with regex I would suggest something like the previous posters are commenting on

  REReplace(usersfilename,"[^a-zA-Z0-9]","","ALL")

These samples assume you will add the ".pdf" and only allows letters and numbers. If you need underscores or the period it would look like this...

  REReplace(usersfilename,"[^a-zA-Z0-9\._]","","ALL")

I am not a regex guru, if I have one of these wrong I am sure several will jump in and correct me :)


Pound signs are not legal within filenames on the web. They are used for in-page anchor targets:

<a name="target">

So if you have file#name.pdf, the browser is actually looking for the file "file" and the internal anchor "name.pdf".

Yes, you will have to rename your files on upload.


I can't comment yet, but Kevink's solution is good unless you need to perserve what you're replacing.

We ran into an instance where we needed to rename the filename but the filename needed to be somewhat preserved (user requirement). Simply removing special characters wasn't an option. As a result we had to handle each replace individually, something like.

<cfset newName = replace(thisFile, "##", "(pound)", "All")>
<cfset newName = replace(newName , "&", "(amp)", "All")>
<cffile action="rename"source = "#ExpandPath("\uploads\#thisFolder#\#thisFile#")#" destination = "#newName#">


Probably you would have to replace # with ## to avoid this, I think this is caused because # is figured as Coldfusion keyword.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜