In the previous article, we discussed RFC 959, which defines how an FTP server should work. We also walked through some of the commands that are used in this protocol. In this article, we will be creating an example FTP server in Delphi.
The Implementation of an FTP Server - Deleting Files (Page 3 of 6 )
The client sends the name of the file to be deleted in the APathName variable. The procedure below basically takes that pathname and checks to see if the file exists before deleting it.
procedure TForm1.IdFTPServer1DeleteFile(ASender:TIdFTPServerContext;
const APathName: String);
begin
if fileexists(APathName) then
begin
DeleteFile(APathName);
end;
end;
This event gets fired when the client wants to verify whether a file exists. The procedure uses the file exists() function to carry out the request:
procedure TForm1.IdFTPServer1FileExistCheck(ASender:TIdFTPServerContext;
const APathName: String; var VExist: Boolean);
begin
if fileexists(APathName) then
begin
VExist:=true;
end
else
begin
VExist:=False;
end;
end;
procedure TForm1.IdFTPServer1GetFileDate(ASender: TIdFTPServerContext;
const AFilename: String; var VFileDate: TDateTime);
var
fdate:tdatetime;
begin
//put the file date in a variable
fdate:= FileAge(AFilename);
if not (fdate=-1) then begin
VFileDate:=fdate;
end;
end;
We use the FindNext and FindFirst functions to get the requested file size as below:
procedure TForm1.IdFTPServer1GetFileSize(ASender:TIdFTPServerContext;
const AFilename: String; var VFileSize: Int64);
Var
LFile : String;
rec:tsearchrec;
ASize: Int64 ;
begin
LFile := setslashes(homedir + AFilename );
try
if FindFirst(Lfile, faAnyFile, rec) = 0 then repeat
Asize:=rec.Size;
until FindNext(rec) <> 0;
finally
FindClose(rec);
end;
if Asize > 1 then
VFileSize:= Asize
else
VFilesize:=0;
end;