Solved

ColdFusion Upload Whole Folder and all of its contents

Posted on 2010-09-16
22
792 Views
Last Modified: 2012-05-10
I have used the <cffile action="Upload"  command to upload single files.  However, I need a way for a user to upload a whole  folder with images.  Is that possible in coldfusion?

CF Server ver 7
0
Comment
Question by:Albee_J
  • 12
  • 6
  • 4
22 Comments
 
LVL 52

Expert Comment

by:_agx_
Comment Utility
(no points please ... I'm short on time today)

I'm not sure that's even implemented in CF9.  It has a flash uploader that supports multiple file uploads (I'm not sure about "whole folders").

Anyway for MX7, you'd need a 3rd party component.  Preferably flash (or activeX).  There's javascript options too (jquery, ...) But personally I think the flash/activeX ones are more stable.  IIRC, asfusion.com used to provide a free flash control for file uploads.  Though there's plenty of others out there too.  Both commercial and open source. Just search on ColdFusion multiple file uploads.



0
 

Author Comment

by:Albee_J
Comment Utility
This is what I am being told I should be able to accomplish.  It may be easier if you can advise if this is possible.

parse path to get "folderName"  (user have a text box that they enter the path to their folder")

Create the folder on our webserver
Open FTP connection
Loop through users path to get filename

use code to get all files in folder


Somehow I am supposed to be able to use this code

  <cfdirectory action="list" directory="#Arguments.SlideShow#" filter="*.gif|*.png|*.jpg" name="getImages">
  <cfset dirToCreate = #Arguments.SlideShow#>
  <cfdirectory action="create" directory="#dirToCreate#">

  <cfif len(Arguments.SlideShow)>
<cffile action="Upload"
filefield="SlideShow"
destination="E:\Coldfusion8\wwwroot\FMAF-Administrator\Camp-Photos"
nameconflict="makeunique">
       
  <cfftp action = "open"
 username=" "
password="
server=" "
 connection = "images"
 passive="Yes"
 stopOnError = "Yes"
 timeout = "1200"
 retrycount="1">
 
<cfftp  action="PUTFILE"
 connection = "images"
 passive="Yes"
 stopOnError = "Yes"
Localfile="E:\Coldfusion8\wwwroot\FMAF-Administrator\Camp-Photos\#cffile.serverfile#"
remotefile="/www.nutsandboltsfoundation.org/Manufacturing-Camps/#cffile.serverfile#" >

<cfftp  action = "close"
 connection = "images">    

                 <cfset Image = cffile.serverfile>
      <cfelse>
                    <cfset Image = "">
      </cfif>
0
 
LVL 6

Expert Comment

by:reiters
Comment Utility
You code snippet assumes you have FTP access to the user's machine.  Is this the case?  Or maybe the users "path" is on a shared directory ion the server?  I can not think of a way to go and pull the files off of a users machine unless you know all users have a ftp server on their machine.  All transfers must be initiated by the client and typing a path in a text field is not counted as user initiated transfer.  If on the other hand the users files are on a server already and you want them to be sent to another location you may very well be able to ftp to a server with the location given by the user and pull them local.

Another thing not mentioned is if you want the directory to be recursively parsed getting all the sub-folders and their content as well.
0
 
LVL 52

Expert Comment

by:_agx_
Comment Utility
>> 1) parse path to get "folderName"  (user have a text box that they enter the path to their folder")

    Yes, that's doable

>> 2) Create the folder on our webserver

    Again.  That's doable

>> 3) Open FTP connection
>> 4) Loop through users path to get filename

That's where things get questionable.  As reiter's said ... open an FTP connection to where?  If the "user" is another party that also gave you access to _their_ FTP server (username and password) Then yes - you can use CFFTP to go out and grab the files and copy them over to your server.

0
 
LVL 52

Expert Comment

by:_agx_
Comment Utility
BTW: reiters made some very good points.  So I didn't mean to step "on their toes" ;-)

(I'm late for a meeting and will check back later.)
0
 

Author Comment

by:Albee_J
Comment Utility
The users files will be on a shared directory.  I need to get the directory and all of the content.  The folders will only have images which are for a slideshow.
0
 

Author Comment

by:Albee_J
Comment Utility
The users are my co-workers, so it is all internal
0
 

Author Comment

by:Albee_J
Comment Utility
I am unsure of the proper syntax to

parse path to get "folderName" using the URL entered "slideshow" textbox

Create the folder on our webserver (based off of the foldername above)
Open FTP connection
Loop through users path to get files
put all files on new folder on our webserver

0
 
LVL 6

Expert Comment

by:reiters
Comment Utility
If the files are on a shared folder and the web server has access to the same network, then you may be able to CFFILE the files from the users share using the UNC name.  It really depends on your network setup.  If you have UNC naming available then you should be able to copy the entire file structure from source to a destination. You might be able to use cfexecute to initiate the copy so recursion wont be an issue.

Not knowing the exact setup here, I can give you some pointers how to "parse the path"
<cfset last_folder = listgetat(folderName,listlen(folderName,'\'),'\')> will get you the last folder in a path like
d:\user\shared\folders\john

You may have to account for someone typing d:\user\shared\folders\john\ (notice the trailing \)
0
 
LVL 52

Expert Comment

by:_agx_
Comment Utility
>> then you may be able to CFFILE the files from the users share using the UNC name

True.  Keep in mind this means the CF service must be running under an account that has permissions to that share.  By default it runs under "System" (on windows), which does NOT have permissions to shares.  So that part can be tricky.  


>> You may have to account for someone typing d:\user\shared\folders\john\ (notice the trailing \)

It shouldn't be a problem. Most list functions ignore empty elements. So it shouldn't be a problem. But good idea to check the results first.

>> Create the folder on our webserver (based off of the foldername above)

Once you have the base folder name (like "john"), just use cfdirectory to create that same directory locally.

<cfdirectory action="create" directory="c:\local\path\#FolderName#">

0
 

Author Comment

by:Albee_J
Comment Utility
Ok I am trying to walk through this step by step, right now I am trying to do a dump to see if I have even parsed the folder name correct

  <cfif len(Arguments.CampPhoto) NEQ ''>
  <cfset SlideShow = listgetat(SlideShow,listlen(Arguments.SlideShow,'\'),'\')>
 <cfdirectory action="create" directory="E:\Coldfusion8\wwwroot\FMAF-Administrator\Camp-Slideshows\#SlideShow#">
 
</cfif>

This does create the folder based on path entered in text box.  Now I need to loop through all the files and upload them to that folder



 <cfif len(Arguments.CampPhoto) NEQ ''>
  <cfset SlideShow = listgetat(SlideShow,listlen(Arguments.SlideShow,'\'),'\')>
 <cfdirectory action="create" directory="E:\Coldfusion8\wwwroot\FMAF-Administrator\Camp-Slideshows\#SlideShow#">
 
 
 <cffile action="Upload"
filefield="CampPhoto"
destination="E:\Coldfusion8\wwwroot\FMAF-Administrator\Camp-Photos"
nameconflict="makeunique">
       
  <cfftp action = "open"
 username=""
      password=""
      server=""
 connection = "images"
 passive="Yes"
 stopOnError = "Yes"
 timeout = "1200"
 retrycount="1">
 
<cfftp  action="PUTFILE"
 connection = "images"
 passive="Yes"
 stopOnError = "Yes"
Localfile="E:\Coldfusion8\wwwroot\FMAF-Administrator\Camp-Slideshows\#cffile.serverfile#"
remotefile="/www.nutsandboltsfoundation.org/assets/images/Manufacturing-Camp-Photos/#cffile.serverfile#" >

<cfftp  action = "close"
 connection = "images">    

                 <cfset Image = cffile.serverfile>
      <cfelse>
                    <cfset Image = "">
      </cfif>
 
</cfif>
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 6

Expert Comment

by:reiters
Comment Utility
Where are the source files? Are they on the users machine? Is this on a server that has fuzebox on it?
0
 

Author Comment

by:Albee_J
Comment Utility
The files are on an exchange server within our building. The source files are images.  I am getting close but I know my looping is off.

<cfif len(Arguments.SlideShow) NEQ ''>
 
   <cfdirectory action="list" directory="#Arguments.SlideShow#" filter="*.gif|*.png|*.jpg" name="getImages">
     
  <cfset SlideShowFolder = listgetat(SlideShow,listlen(Arguments.SlideShow,'\'),'\')>
 <cfdirectory action="create" directory="E:\Coldfusion8\wwwroot\FMAF-Administrator\Camp-Slideshows\#SlideShowFolder#">
 
 
<cfloop
   index = "#i#"
   list = "getImages"
   delimiters = "1">
 
 <cffile action="Upload"
filefield="#i#"
destination="E:\Coldfusion8\wwwroot\FMAF-Administrator\Camp-Slideshows"
nameconflict="makeunique">
       
  <cfftp action = "open"
 username=""
      password=""
      server=""
 connection = "images"
 passive="Yes"
 stopOnError = "Yes"
 timeout = "1200"
 retrycount="1">
 

<cfftp  action="PUTFILE"
 connection = "images"
 passive="Yes"
 stopOnError = "Yes"
Localfile="E:\Coldfusion8\wwwroot\FMAF-Administrator\Camp-Slideshows\#SlideShowFolder#\#cffile.serverfile#"
remotefile="/www.nutsandboltsfoundation.org/assets/images/Manufacturing-Camp-Photos/#SlideShowFolder#/#cffile.serverfile#" >
</cfloop>

<cfftp  action = "close"
 connection = "images">    

                 <cfset Image = cffile.serverfile>
      <cfelse>
                    <cfset Image = "">
      </cfif>
0
 

Author Comment

by:Albee_J
Comment Utility
Close but not quite, I can see all the files, but I can't get them to upload

 
  <cfif len(Arguments.SlideShow) NEQ ''>
 
   <cfdirectory action="list" directory="#Arguments.SlideShow#" filter="*.gif|*.png|*.jpg" name="getImages">
     
  <cfset SlideShowFolder = listgetat(SlideShow,listlen(Arguments.SlideShow,'\'),'\')>
 <cfdirectory action="create" directory="E:\Coldfusion8\wwwroot\FMAF-Administrator\Camp-Slideshows\#SlideShowFolder#">
 
 
<cfloop query = "getImages">

  <cfdump var="#getImages#">
  <cfabort>
 
 <cffile action="Upload"
filefield="getImages"
destination="E:\Coldfusion8\wwwroot\FMAF-Administrator\Camp-Slideshows"
nameconflict="makeunique">
       
  <cfftp action = "open"
 username=" "
      password=" "
      server=" "
 connection = "images"
 passive="Yes"
 stopOnError = "Yes"
 timeout = "1200"
 retrycount="1">
 

<cfftp  action="PUTFILE"
 connection = "images"
 passive="Yes"
 stopOnError = "Yes"
Localfile="E:\Coldfusion8\wwwroot\FMAF-Administrator\Camp-Slideshows\#SlideShowFolder#\#cffile.serverfile#"
remotefile="/www.nutsandboltsfoundation.org/assets/images/Manufacturing-Camp-Photos/#SlideShowFolder#/#cffile.serverfile#" >
</cfloop>

<cfftp  action = "close"
 connection = "images">    

                 <cfset Image = cffile.serverfile>
      <cfelse>
                    <cfset Image = "">
      </cfif>
0
 
LVL 6

Expert Comment

by:reiters
Comment Utility
is this what you are looking for.  If you can see the source files using cfdirectory and create a folder at the destination then you should not need FTP from the way im seeing it.  i could be wrong.
<cfif len(Arguments.SlideShow) NEQ ''>

	<cfdirectory action="list" directory="#Arguments.SlideShow#" filter="*.gif|*.png|*.jpg" name="getImages">
	<cfset SlideShowFolder = listgetat(SlideShow,listlen(Arguments.SlideShow,'\'),'\')>
	<cfdirectory action="create" directory="E:\Coldfusion8\wwwroot\FMAF-Administrator\Camp-Slideshows\#SlideShowFolder#">
	<cfloop query="getImages">
		<cffile action="COPY" source="#Arguments.SlideShow#\#getImages.name#" destination="E:\Coldfusion8\wwwroot\FMAF-Administrator\Camp-Slideshows\#SlideShowFolder#\#getImages.name#">
	</cfloop>
<cfelse>
	<cfset Image = "">
</cfif>

Open in new window

0
 

Author Comment

by:Albee_J
Comment Utility


but once all of the files are copied, I need to move them up to the server.  I may be confused because the upload command has local and remote

I first have to pull it from the users local, then to the remote

/www.nutsandboltsfoundation.org/assets/images/Manufacturing-Camp-Photos/#SlideShowFolder#/#cffile.serverfile#
0
 
LVL 6

Accepted Solution

by:
reiters earned 500 total points
Comment Utility
UPLOAD is specifically for when your user clicks browse on tha form and picks a file from their own machine.

I'm trying.  We'll get there.

I assume you removed the server, username, and password from the code for security reasons and you will be putting that back in.
<cfif len(Arguments.SlideShow) NEQ ''>

	<cfdirectory action="list" directory="#Arguments.SlideShow#" filter="*.gif|*.png|*.jpg" name="getImages">

	<cfset SlideShowFolder = listgetat(SlideShow,listlen(Arguments.SlideShow,'\'),'\')>

	<cfdirectory action="create" directory="E:\Coldfusion8\wwwroot\FMAF-Administrator\Camp-Slideshows\#SlideShowFolder#">

	<cfftp action="open" username=" " password=" " server=" " connection="images" passive="Yes" stopOnError = "Yes" timeout = "1200" retrycount="1">

	<cfftp action="changedir" directory="/www.nutsandboltsfoundation.org/assets/images/Manufacturing-Camp-Photos/" connection="images">

	<cfftp action="createdir" directory="#SlideShowFolder#" connection="images">

	<cfftp action="changedir" directory="/www.nutsandboltsfoundation.org/assets/images/Manufacturing-Camp-Photos/#SlideShowFolder#/" connection="images">

	<cfloop query="getImages">

		<cfftp action="PUTFILE" connection="images" passive="Yes" stopOnError="Yes" Localfile="#Arguments.SlideShow#\#getImages.name#" remotefile="#getImages.name#">

	</cfloop>

	<cfftp  action="close" connection="images">

	<cfset Image = "">

<cfelse>

	<cfset Image = "">

</cfif>

Open in new window

0
 

Author Comment

by:Albee_J
Comment Utility
Correct on the server information
0
 

Author Comment

by:Albee_J
Comment Utility
I got a  An error occurred during the FTP createdir operation.
Error: 550 Can't create directory. Permission denied .   which is good, working just means the permission need to be changed.
0
 

Author Comment

by:Albee_J
Comment Utility
Success!

The final code


  <!---Slide Show--->
 
 
  <cfif len(Arguments.SlideShow) NEQ ''>
 
   <cfdirectory action="list" directory="#Arguments.SlideShow#" filter="*.gif|*.png|*.jpg" name="getImages">
     
  <cfset SlideShowFolder = listgetat(SlideShow,listlen(Arguments.SlideShow,'\'),'\')>
 <cfdirectory action="create" directory="E:\Coldfusion8\wwwroot\FMAF-Administrator\Camp-Slideshows\#SlideShowFolder#">
 
 
<cfftp action="open"
 username=" "
password=" "
server=" "
connection="images"
passive="Yes"
stopOnError = "Yes"
timeout = "1200"
retrycount="1">

      <cfftp action="changedir" directory="/www.nutsandboltsfoundation.org/Manufacturing-Camps/" connection="images">
      <cfftp action="createdir" directory="#SlideShowFolder#" connection="images">
      <cfftp action="changedir" directory="/www.nutsandboltsfoundation.org/Manufacturing-Camps/#SlideShowFolder#/" connection="images">
      <cfloop query="getImages">
            <cfftp action="PUTFILE" connection="images" passive="Yes" stopOnError="Yes" Localfile="#Arguments.SlideShow#\#getImages.name#" remotefile="#getImages.name#">
      </cfloop>


<cfftp  action = "close"
 connection = "images">    

                 <cfset Image = "">
      <cfelse>
                    <cfset Image = "">
      </cfif>
0
 

Author Closing Comment

by:Albee_J
Comment Utility
Thank you soooooooo much!
0
 
LVL 6

Expert Comment

by:reiters
Comment Utility
Glad I could help
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

CFGRID Custom Functionality Series -  Part 1 Hi Guys, I was once asked how it is possible to to add a hyperlink in the cfgrid and open the window to show the data. Now this is quite simple, I have to use the EXT JS library for this and I achiev…
Recently while working on a project I got a very annoying cfdocument has no body error message. I had never seen this error before. So I checked the code. The code was pretty simple; it was Just showing me the cfdocumnt tag and inside that tag a …
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

771 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