RE: FTP from vfp

Author: J. Chad Bourque

Posted: 2002-06-04 at 11:20:12

Rafael,

Here's a little program I wrote a few months ago to push a file onto a

web server via FTP. I'm sure it could easily be modified to pull a

file. I believe I got the bulk of this code (and modified it) from

http://www.news2news.com/vfp/?group=3D-1&=3D0 so if you need more info, =

you

might check there.

HTH

J. Chad Bourque, MCP

Fort=E9 Incorporated

chad@teche.net=20

#DEFINE INTERNET_INVALID_PORT_NUMBER 0 =20

#DEFINE INTERNET_OPEN_TYPE_DIRECT 1 =20

#DEFINE INTERNET_SERVICE_FTP 1 =20

#DEFINE FTP_TRANSFER_TYPE_ASCII 1 =20

#DEFINE FTP_TRANSFER_TYPE_BINARY 2 =20

PUBLIC hOpen, hFtpSession =20

DO decl =20

* select FTP connection providing you an appropriate access level =20

* in all cases it can not be any "anonymous" access =20

IF connect2ftp ("nnn.nnn.nnn.nnn", "username", "password")

*!* Where nnn.nnn.nnn.nnn is the IP address =20

* select the existing source and valid target file names =20

* these names are as an example only =20

lcSource =3D "c:\dev\CurrentNovus.html" =20

lcTarget =3D "/webroot/sitesecure/CurrentAddress.html"=20

*!* lcRenamed =3D "/webroot/sitesecure/CurrentAddress.ht~"=20

*!* =20

* default transfer type selected - =20

* the file is transfered exactly as it is =20

IF FtpPutFile (hFtpSession, lcSource,;=20

lcTarget, FTP_TRANSFER_TYPE_BINARY, 0) =3D 1=20

*!* * if file (lcRenamed) already exists, most probably it

would be replaced=20

*!* * with the new one during the renaming=20

*!* * at least my FTP server acts like this=20

*!* ? FtpRenameFile (hFtpSession, lcTarget, lcRenamed)=20

ENDIF=20

=3D InternetCloseHandle (hFtpSession) =20

=3D InternetCloseHandle (hOpen) =20

ENDIF =20

PROCEDURE decl =20

DECLARE INTEGER InternetOpen IN wininet.dll; =20

STRING sAgent,; =20

INTEGER lAccessType,; =20

STRING sProxyName,; =20

STRING sProxyBypass,; =20

STRING lFlags =20

=20

DECLARE INTEGER InternetCloseHandle IN wininet.dll INTEGER hInet =20

DECLARE INTEGER InternetConnect IN wininet.dll; =20

INTEGER hInternetSession,; =20

STRING sServerName,; =20

INTEGER nServerPort,; =20

STRING sUsername,; =20

STRING sPassword,; =20

INTEGER lService,; =20

INTEGER lFlags,; =20

INTEGER lContext =20

DECLARE INTEGER FtpPutFile IN wininet.dll; =20

INTEGER hConnect,; =20

STRING lpszLocalFile,; =20

STRING lpszNewRemoteFile,; =20

INTEGER dwFlags,; =20

INTEGER dwContext =20

DECLARE INTEGER FtpRenameFile IN wininet.dll;=20

INTEGER hConnect,;=20

STRING lpszExisting,;=20

STRING lpszNew=20

RETURN =20

FUNCTION connect2ftp (strHost, strUser, strPwd) =20

* open access to Inet functions =20

hOpen =3D InternetOpen ("vfp", INTERNET_OPEN_TYPE_DIRECT, 0, 0, 0) =

=20

IF hOpen =3D 0 =20

? "Unable to get access to WinInet.Dll" =20

RETURN .F. =20

ENDIF =20

* connect to FTP =20

hFtpSession =3D InternetConnect (hOpen, strHost,

INTERNET_INVALID_PORT_NUMBER,; =20

strUser, strPwd, INTERNET_SERVICE_FTP, 0, 0) =20

IF hFtpSession =3D 0 =20

* close access to Inet functions and exit =20

=3D InternetCloseHandle (hOpen) =20

? "FTP " + strHost + " is not available" =20

RETURN .F. =20

ELSE =20

? "Connected to " + strHost + " as: [" + strUser + ", *****]"

ENDIF =20

RETURN .T.

©2002 J. Chad Bourque