Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

cfftp upload folder with subfolders in a single operation.

Explorer ,
Jan 23, 2010 Jan 23, 2010

Hi everybody,

I have a folder full of subfolders with images in them and I want to upload them to an ftp server. Is this possible to instruct coldfusion to upload this folder with all its sublfolders and contents?

Thank you in advance,

Yannis

5.9K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

LEGEND , Jan 24, 2010 Jan 24, 2010

The thing is, with recursion, one doesn't need to worry about any level of the hierarchy other than the "current" one, and then making the recursive call at the right point.  The act of recursing sorts out the rest of it automatically.

One trap I have seen people getting bogged down with when attempted something recursive is that they try to hold the entire "end result" in their heads (like traversing the sub-sub-sub-dirs), whereas there's simply no need.  One only ever has to focus on getting on

...
Translate
Explorer ,
May 03, 2010 May 03, 2010

Hello friends,

I'm back again and it is for good this time. What I mean is that I found the time at last (AT LAST) to finish this image manipulation app of mine. I decided to fully implement Coldfusion without plugins. I really need to thank BKBK for the nice FTP solution but I guess that the solution I implemented is nice and clean as well.

Just in order to thank you (all) about your contribution I provide the code here. I hope it proves to be usefull to some of you.

There is much to be done though. I should implement error handling, cfthread maybe, any many-many other nice things, but for now that the functionality is ready I will leave it as is. Later maybe...

(I translated the messages for greek to english for your convinience right now, so excuse me for the typos!)

Thank you all (Adam, Dan, BKBK),

Yannis

<!--- -------------------------------------------------------------------
Name:..................................photoConverter.cfm
Author:................................Yannis Develekos
Description:...........................Photos converting application
Created:...............................23 Jan 2010
Previous Modified:.....................N/A
Last Modified:.........................N/A
Previous Modification Description:.....N/A
Last Modification Description:.........N/A
-------------------------------------------------------------------- --->

<!---
Template logic:
  - Creates the watermark and loads it in memory  (code from http://www.bennadel.com/blog/775-learning-coldfusion-8-cfimage-part-iii-watermarks-and-transparency.... - Thank you Ben)
  - Checks all the contents of a folder and loads the images one by one (cfdirectory action="list").
  - Opens ftp connection
  - Checks for the existense of the folder on the web server and if it does not exist it creates it
  - If it is a valid image, it applies the watermark after calculating position and dimensions
  - Uploads the images to the server
  - Uploads the xml file needed for the DB
  - Closes the ftp connection
  - Outputs statistics to the server
--->

<!--- Name the working directory --->
<cfset imgDirectory = "C:\Inetpub\ftproot\images\" >

<!--- Create the variable to hold number of photos processed --->
<cfset photoNumber = 0 >

<!--- Create the variable to hold number of images already existed --->
<cfset imagesExisted = 0 >

<!--- Let give the user something to read (cfflush) --->
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Σύστημα Διαχείρισης Φωτογραφιών - Aptown Real Estate</title>
</head>

<body style="font:normal bold 12px/30px 'Trebuchet MS', Arial, Helvetica, sans-serif;">
<h1 style="color:#339;">Image manipulation system - Aptown Real Estate</h1>
<h2>System Initialization</h2>
<h3 style="color:#339;">Please wait...</h3>
<cfflush>

<!---~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Create a new image for the watermark with the given
dimensions and give it a very light gray canvas color.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ --->

<cfset objWatermark = ImageNew(
"",
300,
70,
"rgb",
"##F0F0F0"
) />

<!---
Set the drawing color. This will be the color
for all image manipulations going forward.
--->
<cfset ImageSetDrawingColor(
objWatermark,
"black"
) />

<!--- Draw the rectangle. --->
<cfset ImageDrawRect(
objWatermark,
4,
4,
(objWatermark.GetWidth() - 8),
(objWatermark.GetHeight() - 8)
) />

<!---
Set text drawing anti aliasing. This will give are
text a smoother rendered look (rather than jagged
text you would see on a web page).
--->
<cfset ImageSetAntialiasing(
objWatermark,
"on"
) />

<!--- Create text attributes. --->
<cfset objAttributes = {
Font = "Arial",
Size = "60",
Style = "Bold"
} />

<!--- Draw the watermark text onto our watermark image. --->
<cfset ImageDrawText(
objWatermark,
"APTOWN",
15,
58,
objAttributes
) />

<!--- save watermark width and height for later use --->
<cfset watermarkWidth = objWatermark.GetWidth() />
<cfset watermarkHeight = objWatermark.GetHeight() />

<!---~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     Watermark created!
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ --->

<!--- use cfdirectory to create photos list --->
<cfdirectory
action="list"
directory="#imgDirectory#"
recurse="yes"
name="ourPhotos" />

<!--- open ftp connection to the web server --->
<cfftp action = "open"
    username = "aptown"
    connection = "ftpToApollo"
    password = "Asdf1234"
    server = "ftp.microelements.gr"
    stopOnError = "Yes">

<!--- loop over the contents of cfdirectory --->
<cfloop query="ourPhotos">

<!--- Is it a folder? --->
<cfif ourPhotos.type IS "Dir">

  <!--- Is it available on the web server? --->
  <cfftp action="existsdir" connection="ftpToApollo" directory="/images/photos/#ourPhotos.name#">

  <!--- If it's not, create it now --->
  <cfif cfftp.returnValue eq 'no'>
   <cfftp action="createdir" connection="ftpToApollo" directory="/images/photos/#ourPhotos.name#">
  </cfif>
 
  <!--- and save it to a variable to use it in subsequent file operation in the same folder --->
  <cfset currentFolder = "#ourPhotos.name#" >

<!--- if it was not a folder this should be an image --->
<cfelse>
  <!--- setup the correct filename now and --->
  <!--- get rid of the invalid characters in the image name --->
  <cfset photoName = LCase(REReplaceNoCase("#ourPhotos.name#","[^0-9A-Za-z\.\,]","i","ALL")) />

  <!--- maybe it has already been uploaded to the server --->
  <!--- to find out check for a thumb_imagename.jpg existense --->
  <cfif FileExists("#ourPhotos.directory#\thumb_#photoName#") OR Find("thumb_","#ourPhotos.name#") OR Find("large_","#ourPhotos.name#") >

   <!--- update imagesExisted variable and move to the next ourPhotos entry --->
   <cfset imagesExisted = imagesExisted + 1 / 3>
  <cfelse>

   <!--- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      run the image manipulation process
   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ --->

   <!--- Is it a valid image? --->
   <cfif IsImageFile("#ourPhotos.directory#\#ourPhotos.name#")>

    <!--- Create a ColdFusion image from an existing JPEG file. --->
    <cfimage source="#ourPhotos.directory#\#ourPhotos.name#" name="myImage" />
   
    <!--- Turn on antialiasing to improve image quality. --->
    <cfset ImageSetAntialiasing(myImage,"on")>
   
    <!--- create the large photo --->
    <cfset ImageScaleToFit(myImage,420,"","highestQuality") />
    
    <!---
    When we paste the watermark onto the photo, we don't
    want it to be fully visible. Therefore, let's set the
    drawing transparency to 60% before we paste.
    --->
    <cfset ImageSetDrawingTransparency(
     myImage,
     60
     ) />
 
    <!---
    Paste the manually created watermark image onto the photo.
    This time, we don't need to turn on any anti aliasing since
    the watermark has solid borders. The anti alisasing only
    helps us when the pasted image has anti aliased perimeter.
    --->
    <cfset ImagePaste(
     myImage,
     objWatermark,
     ((myImage.GetWidth() - watermarkWidth) / 2),
     ((myImage.GetHeight() - watermarkHeight) / 2)
     ) />
   
   
    <!--- and write it to a file --->
    <cfimage source="#myImage#" action="write" destination="#ourPhotos.directory#\large_#photoName#" overwrite="true" />
   
    <!--- upload the image to the web server --->
    <!--- The image will be put into the specified directory of the FTP server  --->
    <cfftp 
     connection = "ftpToApollo"
     action = "putFile" 
     name = "uploadImage" 
     transferMode = "binary" 
     localFile = "#ourPhotos.directory#\large_#photoName#" 
     remoteFile = "/images/photos/#currentFolder#/large_#photoName#"
     failifexists="no">
    
    <!--- create the thumb --->
    <cfset ImageScaleToFit(myImage,50,"","highestQuality") >

    <!--- and write it to the disk too --->
    <cfimage source="#myImage#" action="write" destination="#ourPhotos.directory#\thumb_#photoName#" overwrite="true" />

    <!--- and upload it too --->
    <cfftp 
     connection = "ftpToApollo"
     action = "putFile" 
     name = "uploadThumb" 
     transferMode = "binary" 
     localFile = "#ourPhotos.directory#\thumb_#photoName#" 
     remoteFile = "/images/photos/#currentFolder#/thumb_#photoName#"
     failifexists="no">

    <cfset photoNumber = photoNumber + 1 />
    <cfoutput>#photoNumber# - #photoName#</cfoutput><br />
   
    <!--- Let's flush output to the user --->
    <cfflush>
   <cfelse>
    <!--- let the user know that we have an invalid image --->
    <hr />
    <cfoutput>
     <h3 style="color:red;">ΑΤΤΕΝΤΙΟΝ! The file #ourPhotos.directory#\#ourPhotos.name# is not a valid image!</h3>
    </cfoutput>
    <hr />
    <cfprocessingdirective suppresswhitespace="no">
   
     <!--- Send mail to me now --->
     <cfmail
      subject="Problem with aptown image file process"
      from='"Aptown Administrator" <johnny@aptown.eu>'
      to="john@microelements.gr"
      cc="podaras@aptown.eu"
      type="html">
      <h1>The image manipulation system reports an error</h1>
      <h2>Problem with image file</h2>
      <h3>The file #ourPhotos.directory#\#ourPhotos.name# is not a valid image!</h3>
     
     </cfmail>
    </cfprocessingdirective>
   </cfif>
  </cfif>
</cfif>
 
</cfloop>

<!--- compress the xml file --->
<cfzip
source="#imgDirectory#/orbit.xml"
    file = "#imgDirectory#/orbit.zip"
    action = "zip"
    overwrite = "yes"
    recurse = "no">

<!--- and upload it to the server --->
<cfftp 
connection = "ftpToApollo"
action = "putFile" 
name = "uploadXML" 
transferMode = "binary" 
localFile = "#imgDirectory#\orbit.zip" 
remoteFile = "/upload/orbit.zip"
failifexists="no">

<!--- we do not need the ftp connection any more --->
<cfftp action="close" connection="ftpToApollo">

<!--- some nice statistics for the user --->
<h1 style="color:#339;">Statistics:</h1>

<!--- number of images uploaded --->
<cfswitch expression="#photoNumber#">
<cfcase value="0">
  No images needed to be uploaded to the web server!<br />
</cfcase>
<cfcase value="1">
  <cfoutput><span style="font-size:large;">1 image</span> uploaded to the web server!</cfoutput><br />
</cfcase>
<cfdefaultcase>
  <cfoutput><span style="font-size:large;">#photoNumber# images</span> uploaded to the web server!</cfoutput><br />
</cfdefaultcase>
</cfswitch>

<!--- number of images already on the server --->
<cfswitch expression="#imagesExisted#">
<cfcase value="0">
  No images already available on the web server!<br />
</cfcase>
<cfcase value="1">
  <cfoutput><span style="font-size:large;">1 image only</span> found on the web server!</cfoutput><br />
</cfcase>
<cfdefaultcase>
  <cfoutput><span style="font-size:large;">#imagesExisted# images</span> already existed on the web server!</cfoutput><br />
</cfdefaultcase>
</cfswitch>

</body>
</html>

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
May 03, 2010 May 03, 2010

The code disappeared!!!

Here is a link to the file!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 05, 2010 May 05, 2010
the solution I implemented is nice and clean as well.

Certainly not techically, compared to the solution I gave above. I have studied your solution. Anyone can compare and make up their own mind, ignoring your talk of a plugin, as Coldfusion itself is a Java application..

However, there is a non-technical side to nice-and-clean. As they say, beauty is in the eye of the beholder.

I had expected you would return to the subject you raised in your private communication. Never mind. Good luck.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
May 06, 2010 May 06, 2010

Hi BKBK,

I'm happy to hear from you again although you made me feel embarrassed about my skills. You see it is really simple...

  1. I am not as experienced as you are. That's for sure. I really believe a solution to be nice and clean when it takes just a couple of lines of code to accomplish what you need and it works fine (error free)! This was my accomplisment (I guess).
  2. You suggest to everyone to compare our solutions but this was not my intention. I know that your solution is far better that mine but mine is easier for me to implement and it works. Anyway, I thought that this was what Adam suggested (and once more it was simpler to me ("beauty is in the eye of the beholder" as you correctly said you heartless )).

So I'm absolutely sure that you are right but that's all I could do for now. Your reply here made me feel sorry about myself, so I will have to try to implement your solution just to understand the difference between the two.

Thanks for your time and the kindness to study my solution and give me feedback.

I appreciate this,

Yannis

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 06, 2010 May 06, 2010

Yannis Develekos wrote:

I'm happy to hear from you again although you made me feel embarrassed about my skills. You see it is really simple...

  1. I am not as experienced as you are. That's for sure. I really believe a solution to be nice and clean when it takes just a couple of lines of code to accomplish what you need and it works fine (error free)! This was my accomplisment (I guess).

Yannis, you made me feel bad for writing that. Only now can I see there was perhaps a harsh tone underneath.

Your code sample shows your skills are as good as mine. The nice thing about our profession is that it's an elephant that no single man can lift alone. So, there are certainly areas in which your skills are superior to mine.

.2.  You suggest to everyone to compare our solutions but this was not my intention. I know that your solution is far better that mine but mine is easier for me to implement and it works. Anyway, I thought that this was what Adam suggested (and once more it was simpler to me ("beauty is in the eye of the beholder" as you correctly said you heartless )).

Others will certainly come here looking for answers. We owe it to ourselves to up our technical game. That is why I compared the solutions. In fact, I do believe it to be good practice to compare solutions, and give feedback. I have often done that in these forums, as I do believe it adds value and quality. However, it's no excuse for me to be overbearing. Sorry about that.

Incidentally, if you're happy with your solution, then ignore my last post. There is a good reason why you should, in any case. You are the author of your code. Someone else is the author of the code I referred to. This is an example of you being in a superior position to me!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
May 06, 2010 May 06, 2010

There is nothing you can do to correct your mistake my friend . Now I will go on as promised and apply your solution during the weekend. I will be back with comments on that.

Besides that you seem to be a well educated man (philosopher maybe) and it's always nice talking to you (even in a harsh tone ).

Thanks again,

Yannis

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 06, 2010 May 06, 2010
LATEST

You're so kind. We can all learn something from Achilles' hubris(ὕβρις)!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources