Solved

FILE NAMING with CFFILE...

Posted on 2006-07-24
15
193 Views
Last Modified: 2013-12-24
I am having a problem with files on a website where I allow users to upload photos.

Its set up to allow them to upload the photos, and it inserts a filename into a MYSQL database that matches their file name exactly, so a CF variable references the photo they uploaded like www.mysite.com/photos/#filename#

The name matches their file name, unless it has to be renamed to avoid conflict.

I would rather auto-assign unique names & insert that auto-assigned name to the database because frequently their file names contain spaces, or # signs which screws everything up.  And also on Linux, part of making it unique is sometimes just changing the filename from .jpg to .JPG which also screws things up...

Is there a way I can make my own system for naming files? I know the system I want to use to create a unique name for each photo, but don't understand how I can attach MY system name to their uploaded photos and insert THAT into the database too?

Does anyone understand what I'm talking about?

Thanks!
Chris
0
Comment
Question by:cmistre
  • 5
  • 4
  • 3
  • +1
15 Comments
 
LVL 12

Expert Comment

by:mkishline
ID: 17170544
After you've done the file upload, you can change the image name to whatever you want with:

<cffile action="rename" source="path_to_current_file" destination="path_to_new_file" />

Since you will already have the path either in the database or in the #cffile# object you can utilize whatever renaming process you want and change the name of the image.
0
 

Author Comment

by:cmistre
ID: 17170722
so first upload it as whatever they want, then rename it?

There is no way to make it upload right the first time?

What about if their file name contains # signs in it?  Won't that screw up coldfusion in a rename process?

Thanks for the quick help on both issues so far, I do appreciate it!

0
 

Author Comment

by:cmistre
ID: 17170733
Oh ya, also, if renaming is the only way to do this, then I would just

<cflocation> to a rename process page, before returning back to the main page so they are instantly & automatically renamed?  Is that how I would go about it this way?

Thanks!
0
 
LVL 12

Expert Comment

by:mkishline
ID: 17170801
so first upload it as whatever they want, then rename it?
That is essentially what you will have to do.

There is no way to make it upload right the first time?
No, coldfusion doesn't have access to change the filename until the file actually exists on the server.

What about if their file name contains # signs in it?  Won't that screw up coldfusion in a rename process?
The upload process is actually able to handle # signs correctly. The file will get uploaded to your server without throwing an error or without any tricky handling on your part.
0
 
LVL 12

Expert Comment

by:mkishline
ID: 17170818
In response to your second question, you could just use a <cflocation to go to a rename process page, but you would need to make sure that you have a record of the path to the file in your database since cffile will no longer exist and you won't be able to reference the the file path from it.
0
 
LVL 13

Expert Comment

by:usachrisk1983
ID: 17170853
CFFILE can name it whatever you want on upload, just append the name to the DESTINATION.  So it'd be something like:

destination="c:\mypictures\random00000.jpg"

No need for a rename.
0
 
LVL 12

Expert Comment

by:mkishline
ID: 17170898
I attempted doing the rename in the upload process a few years ago, but met with inconsistent results from coldfusion. If that's become a stable feature in coldfusion since, then usachrisk's solution is definitely the way you want to go.
0
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 

Author Comment

by:cmistre
ID: 17172946
I don't think the usachrisk solution will work because the user is told to select a file from their computer by browsing & hit upload and the #form.filename# is the name of the file that uploads and the name that gets added to the database to reference later.

Unless I am missing something, it looks like the usachrisk way would work if I wanted every filename to be called random00000.jpg?  I tried using the form field properties so instead of being called filename it was called #myrandomname# set in the beginning of the page based on the user & unique flyer id but I couldn't get it to work any other way than using the users file name.

If you have no other ideas on how to do it originally, I will just have to try it the rename way.

My thoughts were this,

Upload the file as what the users file name is which also gets "inserted" via sql statement as photo1

Instead of going to the main upload page again like it is now, I could divert it to a rename page that does this...

<cffile action="rename" source="/path/#currentfilename#" destination="#newfilename#" />

The current file name variable should still be there if I pass it in a url string such as  
( renamepage.cfm?currentfilename=<cfoutput>#form.filename#</cfoutput> )

Then set the mysql database to make photo1 = '#newfilename#'

where the newfile name is set in the beginning of the page with a <cfset newfilename='photo#uniqueid#'> where unique id is set according to my own rules.  The at the end of that <cflocation> back to the main page...

Does this logic make sense?

Also is there a way to ban people from uploading photo names with # signs in them, and give them an appropriate error message that says they cannot have # signs in their file name?  This screws up coldfusion like if someone does photo # 1 as the name...

Thanks again.
Chris





 
0
 
LVL 13

Expert Comment

by:usachrisk1983
ID: 17175264
How are you going to get said random name that you were looking for?  Or whatever name it was going to be?  After the form is submitted, the file is uploaded even BEFORE you issue the CFFILE command.  The CFFILE "upload" action actually just takes the file from your webservers temporary location and puts it where you'd like, with the name you'd like.  If you include the file name in the destination, then your cffile.serverFileName (I think that's the right variable) will reference the actual file name that you've given it.
0
 
LVL 25

Expert Comment

by:dgrafx
ID: 17225620
what usachrisk said is correct - he didn't mean all your uploads are hardcoded with random00000.jpg.

you just need to let user know the new filename and insert the new filename to db - is that correct?

just do destination="c:\mypictures\#replace(createUUID(),"-","","ALL")#.jpg" which will give a unique filename or just use
NAMECONFLICT="MakeUnique" which will also give a unique filename.

The new filename is available as #cffile.serverfile# for you to insert into db and to inform user that file has been renamed (if you want)

Do the cffile thing before your db insert so you have cffile.serverfile available to insert
0
 

Author Comment

by:cmistre
ID: 17259917
ok, I'm a little confused about this explanation from dgrafx... but it sounds like you are doing what I need to do with that line

To be clear, the user doesn't need to know the file name, but the database does need to associate the random name given to it to the photo so it can be pulled up correctly when called for.  

Specifically I need to make sure there are no spaces in the file name, so the make unique won't work cause it only renames it the same way with a 1 at the end or something.

So does this line of code generate a unique name by its own system with no spaces or characters?
destination="c:\mypictures\#replace(createUUID(),"-","","ALL")#.jpg

Is there a way to control any part of this name if I want to associate it with a specific member number or flyer number?

The way I was planning on randomizing my own name would work as a re-name directly after the original upload, but it would work better to do it right the first time as this method looks like it does... but please tell me if there is any way to make the code add the member number to the unique name it picks? If not, no big deal but it would be handy for me.

Thanks!



0
 
LVL 25

Expert Comment

by:dgrafx
ID: 17260159
well yes you can do that.
it's very important that you experiment so you can see the result.

like if you have a var named member_number, then just do:
destination="c:\mypictures\#member_number##replace(createUUID(),"-","","ALL")#.jpg"
or
destination="c:\mypictures\#member_number#.jpg" NAMECONFLICT="MakeUnique"
(this is probably your best solution as it just keeps adding onto the end of member_number)

remember that cffile.serverfile is the filename that you will then insert to db
if you ever need to remove spaces from something replace(var,chr(32),"","all")
0
 
LVL 25

Accepted Solution

by:
dgrafx earned 500 total points
ID: 17408063
I believe I gave the correct and best solution to this posters issue, and I should get the points.
0
 

Author Comment

by:cmistre
ID: 17412692
Hi,

I'm sorry I didn't reply to this... I had to leave unexpectedly and didn't get a chance to experiment with any of this.

I will go ahead and accept dgrafx and hopefully it will work as intended, thanks!

Chris
0

Featured Post

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Join & Write a Comment

Have you ever sent email via ColdFusion and thought of tracking this mail to capture the exact date and time when the message was opened ?  If yes, then this article is for you ! First we need a table user_email with columns user_id , email , sub…
If you don't have the right permissions set for your WordPress location in IIS, you won't be able to perform automatic updates. Here's how to fix the problem.
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now