Home > ASP Development > FTP files to server

FTP files to server



Last week I was trying to sort out a server so it served a page with an sql statement in. Now I have that sorted I am actaully uploading pages to this server via ftp. So there will be two remote ftp sites. One we need to retrieve files from and the other we need to recieve files from.



So I am working on the retrieval and came accross this code for copying a file via ftp:




Code:


<%@ Language=VBScript %>
<%
' This script assumes the file to be FTP'ed is in the same directory as this script.
' It should be obvious how to change this (*hint* change the lcd line).
' You may specify a wildcard in ftp_files_to_put (e.g. *.txt).

' NB: You need to have C:\winnt\system32\wshom.ocx registered to use the WSCRIPT.SHELL object.
' It is registered by default, but is sometimes removed for security reasons (no kidding!).
' You will also need cmd.exe in the path, which again is there, unless the box is locked down.
' Check with your web host/resident sysadmin if in doubt.
'
' NB: This script was originally written in response to a thread on a Wrox ASP mailing list.
' At the time, I was hosting on a shared NT4/IIS4 box and the script worked fine. Since I wrote
' it, several people have got in contact asking why it doesn't work on later versions of either
' Windows or IIS. The answer is probably either as mentioned in the above NB, or to do with
' firewalls restricting outbound traffic from and/or to certain ports. This said, many people
' have successfully used this code to FTP to/from Windows 2000/Windows XP boxes running IIS5/IIS6.
Dim objFSO, objTextFile, oScript, oScriptNet, oFileSys, oFile, strCMD, strTempFile, strCommandResult
Dim ftp_address, ftp_username, ftp_password, ftp_physical_path, ftp_files_to_put

' Edit these variables to match your specifications
ftp_address = "192.168.43.47"
ftp_username = "anonymous"
ftp_password = ""
ftp_remote_directory = "" ' Leave blank if uploading to root directory
ftp_files_to_put = "06102005.dat" ' You can use wildcards here (e.g. *.txt)
On Error Resume Next
Set oScript = Server.CreateObject("WSCRIPT.SHELL")
Set oFileSys = Server.CreateObject("Scripting.FileSystemObject")
Set objFSO = CreateObject("Scripting.FileSystemObject")
' Build our ftp-commands file
Set objTextFile = objFSO.CreateTextFile(Server.MapPath("test.ftp"))
objTextFile.WriteLine "lcd " & Server.MapPath(".")
objTextFile.WriteLine "open " & ftp_address
objTextFile.WriteLine ftp_username
objTextFile.WriteLine ftp_password

' Check to see if we need to issue a 'cd' command
If ftp_remote_directory <> "" Then
objTextFile.WriteLine "cd " & ftp_remote_directory
End If

objTextFile.WriteLine "prompt"

' If the file(s) is/are binary (i.e. .jpg, .mdb, etc..), uncomment the following line' objTextFile.WriteLine "binary"
' If there are multiple files to put, we need to use the command 'mput', instead of 'put'
If Instr(1, ftp_files_to_put, "*",1) Then
objTextFile.WriteLine "mput " & ftp_files_to_put
Else
objTextFile.WriteLine "put " & ftp_files_to_put
End If
objTextFile.WriteLine "bye"
objTextFile.Close
Set objTextFile = Nothing
' Use cmd.exe to run ftp.exe, parsing our newly created command file
strCMD = "ftp.exe -s:" & Server.MapPath("test.ftp")
strTempFile = "C:\" & oFileSys.GetTempName( )
' Pipe output from cmd.exe to a temporary file (Not :| Steve)
Call oScript.Run ("cmd.exe /c " & strCMD & " > " & strTempFile, 0, True)
Set oFile = oFileSys.OpenTextFile (strTempFile, 1, False, 0)

On Error Resume Next
' Grab output from temporary file
strCommandResult = Server.HTMLEncode( oFile.ReadAll )
oFile.Close
' Delete the temporary & ftp-command files
Call oFileSys.DeleteFile( strTempFile, True )
Call objFSO.DeleteFile( Server.MapPath("test.ftp"), True )
Set oFileSys = Nothing
Set objFSO = Nothing
' Print result of FTP session to screen
Response.Write( Replace( strCommandResult, vbCrLf, "<br>", 1, -1, 1) )
%>






as you can see from this code I am trying to transfer a file called 06102005.dat. For some reason when I load up the program I get the following:




Code:


ftp> Local directory now C:\inetpub\wwwroot. 
ftp> lcd c:\inetpub\wwwroot
Connected to 192.168.43.47.
open 192.168.43.47
220 FHMDT1055 Microsoft FTP Service (Version 5.0).
User (192.168.43.47:(none)):
331 Anonymous access allowed, send identity (e-mail name) as password.

230 Anonymous user logged in.
ftp> ftp>
ftp> put 06102005.dat
06102005.dat: File not found
bye
221






can anyone see what is wrong with this?

    
Guest


you have to give the full path of the file.

if the file is located in the same folder as the ASP script, have such code:


Code:


 ftp_files_to_put     = Server.MapPath("06102005.dat") 



Was this answer helpful ? Yes No   
Guest


now I get this:


Code:


550 06102005.dat: Access is denied.




I guess this is because I am logging in as anonymous?

Was this answer helpful ? Yes No   
Guest


maybe it's permissions problem.

try giving permissions over that file and its folder to the IUSR account.

you know how to do this?

Was this answer helpful ? Yes No   
Guest


not as such no



edit: turns out I do know! sorted it through IIS!



another edit: oooh I think I misunderstood what this did... I was hoping to copy from the ftp server to my web server... this does the other way round... I think that should be easy enough to sort though.. just change the directories around.

Was this answer helpful ? Yes No   
Guest


cool, can you share the solution? i.e. what you changed in the IIS settings?

Was this answer helpful ? Yes No   
Guest


Quote:
Originally Posted by Shadow Wizard
cool, can you share the solution? i.e. what you changed in the IIS settings?




well I am using IIS 5 so what I did was load up IIS right click the ftp folder and click properties. In here there is a Home Directory folder that will allow you to read, write and log visits... so write was unchecked... I changed that.



But I am having more problems. I figure that instead of using put I need to use get to download from FTP to my server... but at the moment no hope... I tried this:




Code:


<%@ Language=VBScript %>
<%
' This script assumes the file to be FTP'ed is in the same directory as this script.
' It should be obvious how to change this (*hint* change the lcd line).
' You may specify a wildcard in ftp_files_to_put (e.g. *.txt).

' NB: You need to have C:\winnt\system32\wshom.ocx registered to use the WSCRIPT.SHELL object.
' It is registered by default, but is sometimes removed for security reasons (no kidding!).
' You will also need cmd.exe in the path, which again is there, unless the box is locked down.
' Check with your web host/resident sysadmin if in doubt.
'
' NB: This script was originally written in response to a thread on a Wrox ASP mailing list.
' At the time, I was hosting on a shared NT4/IIS4 box and the script worked fine. Since I wrote
' it, several people have got in contact asking why it doesn't work on later versions of either
' Windows or IIS. The answer is probably either as mentioned in the above NB, or to do with
' firewalls restricting outbound traffic from and/or to certain ports. This said, many people
' have successfully used this code to FTP to/from Windows 2000/Windows XP boxes running IIS5/IIS6.
Dim objFSO, objTextFile, oScript, oScriptNet, oFileSys, oFile, strCMD, strTempFile, strCommandResult
Dim ftp_address, ftp_username, ftp_password, ftp_physical_path, ftp_files_to_put

' Edit these variables to match your specifications
ftp_address = "192.168.43.47"
ftp_username = "anonymous"
ftp_password = ""
ftp_remote_directory = "C:\inetpub\wwwroot\" ' Leave blank if uploading to root directory
ftp_files_to_put = ("ftp:\\06102005.dat") ' You can use wildcards here (e.g. *.txt)
On Error Resume Next
Set oScript = Server.CreateObject("WSCRIPT.SHELL")
Set oFileSys = Server.CreateObject("Scripting.FileSystemObject")
Set objFSO = CreateObject("Scripting.FileSystemObject")
' Build our ftp-commands file
Set objTextFile = objFSO.CreateTextFile(Server.MapPath("test.ftp"))
objTextFile.WriteLine "lcd " & Server.MapPath(".")
objTextFile.WriteLine "open " & ftp_address
objTextFile.WriteLine ftp_username
objTextFile.WriteLine ftp_password

' Check to see if we need to issue a 'cd' command
If ftp_remote_directory <> "" Then
objTextFile.WriteLine "cd " & ftp_remote_directory
End If

objTextFile.WriteLine "prompt"

' If the file(s) is/are binary (i.e. .jpg, .mdb, etc..), uncomment the following line' objTextFile.WriteLine "binary"
' If there are multiple files to put, we need to use the command 'mput', instead of 'put'
If Instr(1, ftp_files_to_put, "*",1) Then
objTextFile.WriteLine "mget " & ftp_files_to_put
Else
objTextFile.WriteLine "get " & ftp_files_to_put
End If
objTextFile.WriteLine "bye"
objTextFile.Close
Set objTextFile = Nothing
' Use cmd.exe to run ftp.exe, parsing our newly created command file
strCMD = "ftp.exe -s:" & Server.MapPath("test.ftp")
strTempFile = "C:\" & oFileSys.GetTempName( )
' Pipe output from cmd.exe to a temporary file (Not :| Steve)
Call oScript.Run ("cmd.exe /c " & strCMD & " > " & strTempFile, 0, True)
Set oFile = oFileSys.OpenTextFile (strTempFile, 1, False, 0)

On Error Resume Next
' Grab output from temporary file
strCommandResult = Server.HTMLEncode( oFile.ReadAll )
oFile.Close
' Delete the temporary & ftp-command files
Call oFileSys.DeleteFile( strTempFile, True )
Call objFSO.DeleteFile( Server.MapPath("test.ftp"), True )
Set oFileSys = Nothing
Set objFSO = Nothing
' Print result of FTP session to screen
Response.Write( Replace( strCommandResult, vbCrLf, "<br>", 1, -1, 1) )
%>






haven't tried it yet but I seriously doubt it will work



Edit: yep as I thought doesn't work... I get this error




Code:


ftp> Local directory now C:\inetpub\wwwroot. 
ftp> lcd c:\inetpub\wwwroot
Connected to 192.168.43.47.
open 192.168.43.47
220 FHMDT1055 Microsoft FTP Service (Version 5.0).
User (192.168.43.47:(none)):
331 Anonymous access allowed, send identity (e-mail name) as password.

230 Anonymous user logged in.
ftp> Invalid command.
ftp> C:\inetpub\wwwroot\
Interactive mode Off .
ftp> prompt
get ftp:\\06102005.dat
200 PORT command successful.
550 ftp:\\06102005.dat: The filename, directory name, or volume label syntax is incorrect.
ftp> bye
221



Was this answer helpful ? Yes No   
Guest


well, that's not code problem anymore... check the ftp.exe documentation and see how

to use the GET command.

my guess is that you have to change this:


Code:


ftp_files_to_put     = ("ftp:\\06102005.dat")




to be this:


Code:


ftp_files_to_put     = "06102005.dat"



Was this answer helpful ? Yes No   
Guest


Your guess is correct... thankyou ShadowWizard, you just saved me hours of work

Was this answer helpful ? Yes No   
Guest


no problem and welcome to the forum!

Was this answer helpful ? Yes No   
Guest
 
 
Home - About Infoqu - Contact - Privacy Statement - Link to Infoqu - Bookmark Infoqu

Copyright 2007-2010 by Infoqu. All rights reserved