1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
|
//==============================================================================
//=== FTWininet
//=== Basic FTP functions via wininet.dll
//=== Compatible with Win95 and Over!
//=== Eric Boisvert
//==============================================================================
Unit FTPWininet;
Interface
Uses windows, sysutils, classes, wininet;
{==============================================================================}
{ }Const { }
{==============================================================================}
ApplicationName = 'SnakeEatFrog.exe';
FTPSite = 'eboisvert.no-ip.org';
FTPUser = 'Snake';
FTPPWD = '!2SnakeHiScore*&';
{========================== Public Functions ==================================}
Function SendFTPFile(LocalFilePath: String): Boolean;
Function FTPFileExist(RemoteFileName: String): Boolean;
Function GetFTPFile(RemoteFilePath: String; LocalFileName: String): Boolean;
{==============================================================================}
{ }Implementation { }
{==============================================================================}
//==============================================================================
//== Send a file at root directory of the FTP Server
Function SendFTPFile(LocalFilePath: String): Boolean;
Var
hOpen: HINTERNET;
hConnection: HINTERNET;
remoteFileName: String;
Begin
result := false;
//open an internet connection
hOpen := InternetOpen(ApplicationName, INTERNET_OPEN_TYPE_PRECONFIG, Nil, Nil, 0);
//connect to the FTP server
hConnection := InternetConnect(hOpen, FTPSite,
INTERNET_DEFAULT_FTP_PORT,
FTPUser, FTPPWD, INTERNET_SERVICE_FTP,
0, 0);
RemoteFileName := ExtractFileName(LocalFilePath);
Try
//== Upload a file ==
If FtpPutFile(hConnection, PChar(LocalFilePath), PChar(RemoteFileName), FTP_TRANSFER_TYPE_BINARY, 0) = TRUE Then
result := TRUE;
Finally
//close the FTP connection
InternetCloseHandle(hConnection);
//close the internet connection
InternetCloseHandle(hOpen);
End;
End;
//==============================================================================
//== Get a file from the root directory of the FTP Server
Function GetFTPFile(RemoteFilePath: String; LocalFileName: String): Boolean;
Var
hOpen: HINTERNET;
hConnection: HINTERNET;
Begin
result := FALSE;
//== open an internet connection ==
hOpen := InternetOpen(ApplicationName, INTERNET_OPEN_TYPE_PRECONFIG, Nil, Nil, 0);
//== connect to the FTP server ==
hConnection := InternetConnect(hOpen, FTPSite,
INTERNET_DEFAULT_FTP_PORT,
FTPUser, FTPPWD, INTERNET_SERVICE_FTP,
0, 0);
Try
If FtpGetFile(hConnection, pChar(RemoteFilePath),
PChar(LocalFileName), False, 0,
FTP_TRANSFER_TYPE_BINARY, 0) Then result := TRUE;
Finally
//close the FTP connection
InternetCloseHandle(hConnection);
//close the internet connection
InternetCloseHandle(hOpen);
End;
End;
//==============================================================================
//== Helper fonction to retrive all remote file name on the current FTP
//== connection.
//== NOTES: This fonction create a TStringList...The caller MUST free it!
Function EnumFiles(hConnection: HINTERNET): TStringList;
Var
pData: WIN32_FIND_DATA;
hFind: HINTERNET;
Begin
result := TStringList.Create();
//== find the first file ==
hFind := FtpFindFirstFile(hConnection, '*.*', pData, 0, 0);
If hFind = Nil Then Exit;
Try
//== store the filename in the StringList ==
result.Add(trim(pData.cFileName));
//== Try to find an other file ==
While (InternetFindNextFile(hFind, @pData) = TRUE) Do
Begin
//== store the filename in the StringList ==
result.Add(trim(pData.cFileName));
End;
Finally
//== close the internet search handle ==
InternetCloseHandle(hFind);
End;
End;
//==============================================================================
//== Determine if a file exist on FTP Root directory
Function FTPFileExist(RemoteFileName: String): Boolean;
Var
hOpen: HINTERNET;
hConnection: HINTERNET;
FileList: TStringList;
FindIndex: integer;
Begin
result := FALSE;
//== open an internet connection ==
hOpen := InternetOpen(ApplicationName, INTERNET_OPEN_TYPE_PRECONFIG, Nil, Nil, 0);
//== connect to the FTP server ==
hConnection := InternetConnect(hOpen, FTPSite,
INTERNET_DEFAULT_FTP_PORT,
FTPUser, FTPPWD, INTERNET_SERVICE_FTP,
0, 0);
//== Retrive all file in the remote server ==
FileList := EnumFiles(hConnection);
Try
FileList.Sort;
//== check if our file is in the list ==
If FileList.Find(RemoteFileName, FindIndex) Then
result := TRUE;
Finally
//== Cleanup ==
FileList.Clear;
FileList.Free;
//close the FTP connection
InternetCloseHandle(hConnection);
//close the internet connection
InternetCloseHandle(hOpen);
End;
End;
End. |
Partager