FILE NAMING with CFFILE...

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
cmistreAsked:
Who is Participating?
 
dgrafxCommented:
I believe I gave the correct and best solution to this posters issue, and I should get the points.
0
 
mkishlineCommented:
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
 
cmistreAuthor Commented:
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
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

 
cmistreAuthor Commented:
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
 
mkishlineCommented:
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
 
mkishlineCommented:
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
 
usachrisk1983Commented:
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
 
mkishlineCommented:
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
 
cmistreAuthor Commented:
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
 
usachrisk1983Commented:
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
 
dgrafxCommented:
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
 
cmistreAuthor Commented:
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
 
dgrafxCommented:
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
 
cmistreAuthor Commented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.