We help IT Professionals succeed at work.

ColdFusion Upload Whole Folder and all of its contents

Albee_J
Albee_J asked
on
1,037 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
Comment
Watch Question

CERTIFIED EXPERT
Most Valuable Expert 2015

Commented:
(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.



Author

Commented:
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>

Commented:
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.
CERTIFIED EXPERT
Most Valuable Expert 2015

Commented:
>> 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.

CERTIFIED EXPERT
Most Valuable Expert 2015

Commented:
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.)

Author

Commented:
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.

Author

Commented:
The users are my co-workers, so it is all internal

Author

Commented:
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

Commented:
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 \)
CERTIFIED EXPERT
Most Valuable Expert 2015

Commented:
>> 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#">

Author

Commented:
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>

Commented:
Where are the source files? Are they on the users machine? Is this on a server that has fuzebox on it?

Author

Commented:
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>

Author

Commented:
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>

Commented:
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

Author

Commented:


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#
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Author

Commented:
Correct on the server information

Author

Commented:
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.

Author

Commented:
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>

Author

Commented:
Thank you soooooooo much!

Commented:
Glad I could help

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.