05.08.2011, 11:49
(
Last edited by Scottas; 07/08/2011 at 08:35 PM.
Reason: New Version
)
FTP Plugin v0.2a
About
This is a FTP plugin, which let's you to connect to FTP server and do some operations there.
Update v0.2a
Now functions 'Ftp_PutFile' and 'Ftp_GetFile' downloads or uploads files in another thread, so your server wont be frozen, when downloading or uploading. Currently, you can upload only one file ( you need to wait untill files are sent, to start another upload ), same with download.
Added two callbacks:
Code:
forward OnFtpFileSent ( remotefile [ ], localfile [ ] ); forward OnFtpFileReceived ( remotefile [ ], localfile [ ] );
Code:
native Ftp_Connect(server[], username[], password[]); native Ftp_CreateDirectory(directory[]); native Ftp_DeleteFile(file[]); native Ftp_GetFile(remote_file[],local_file[],bool:owerwrite); native Ftp_PutFile(remote_file[],local_file[]); native Ftp_RemoveDirectory(directory[]); native Ftp_RenameFile(file_name[],new_file_name[]); native Ftp_Close(/*No parameters*/); native Ftp_OpenFile(file[],access); native Ftp_ReadFile(handle,buff[],size=sizeof(buff)); native Ftp_WriteFile(handle,string[],size=sizeof(string)); native Ftp_CloseFile(handle); native Ftp_FileExists(file[]);
pawn Code:
public OnGameModeInit ( )
{
new
var;
// Let's connect to FTP server
var = Ftp_Connect ( "serverip", "username", "password" );
if ( !var )
print ( "Can't connect to FTP server" );
// Create directory in FTP root catalog
var = Ftp_CreateDirectory ( "Dir" );
if ( !var )
print ( "Can't create directory" );
// Create another directory, inside it
Ftp_CreateDirectory ( "Dir/New" );
// Create file in it
var = Ftp_OpenFile ( "Dir/New/newfile.txt", FTP_WRITE );
// Let's write something inside it. If file already exists,
// it will be cleared.
Ftp_WriteFile ( var, "This is first line\n" );
Ftp_WriteFile ( var, "This is second line\n" );
// Let's close this file, it's enough
Ftp_CloseFile ( var );
// Now, you can download it to your server.
// We will use 'Ftp_GetFile' function.
// remote_file[] - is the file, we have created
// local_file[] - is new file on our server,
// which contains remote_file content
var = Ftp_GetFile ( "Dir/New/newfile.txt", "scriptfiles/ournewfile.txt", true );
// Note: If you use 'true' in third param, it will overwrite
// file, if it already exists. If you use 'false', file not gonna be
// downloaded, if it exists on local machine
if ( var )
print ( "File prepared for download" );
// Lets delete file from FTP server, we don't need it anymore
var = Ftp_DeleteFile ( "Dir/New/newfile.txt" );
if ( var )
print ( "File successfully removed" );
// And delete 'New' directory
var = Ftp_RemoveDirectory ( "Dir/New" );
if ( var )
print ( "Directory successfully removed" );
// Now let's upload something
var = Ftp_PutFile ( "Dir/uploaded_file.txt", "pawno/include/ftp.inc" );
if ( var )
print ( "file prepared to upload" );
// Let's open our file, but this time for reading
var = Ftp_OpenFile ( "Dir/uploaded_file.txt", FTP_READ );
// Let's read it
new
line [ 100 ];
while ( Ftp_ReadFile ( var, line ) )
print ( line );
// Close file
Ftp_CloseFile ( var );
// Close connection
Ftp_Close ( );
}

Callbacks
Code:
forward OnFtpFileSent ( remotefile [ ], localfile [ ] ); forward OnFtpFileReceived ( remotefile [ ], localfile [ ] );
None yet.
Credits
To RyDeR`, for tutorial.
To Pghpunkid, fo callback tutorial.
To 'Search', for finding all necessary info.
To me, for making a plugin

If you have any suggestions, I'll try to make another version, and add them.
Download