Example: Connect to a FTPS server
Demonstrates how to work with FTP library.
<?v1
require_once ("lib/ftp.inc.v1");
require_once ("lib/utils.inc.v1");
/*
Open FTP connection with optional authentication and TLS.
@param host FTP server
@param user Username
@param pass Password
@param port FTP port, if port is 21
@param fTLS true or 1 if SSL connection is established (FTPS explicit), if fTLS is 2 then a implicit
TLS connection will be established. If fTSL is false or 0, then a pure FTP connection will be established.
@param fVerbose true print return from server to stdout, if fVerbose is a string the callback
function myname (status, param) will be called, param can be a text or array with status information.
@return connection data array.
On error false or error text is returned.
*/
conn = ftp_open ("server.com", "username", "password", 21, true, true);
if (is_array (conn)) {
// Welcome message
print (conn["welcome_msg"]);
// Set data type binary
ftp_type (conn, "I");
// Change directory
ret = ftp_cwd (conn, "logs");
if (ret===true) {
}
else {
print ("CWD failed. ", ret);
}
// Retrieve a file
filename = "text.log";
fh = fopen ("test.log", "w+");
ret = ftp_retr (conn, filename, fh);
fclose (fh);
if (ret===true) {
}
else {
print ("RETR failed. ", ret);
if (is_file (filename))
unlink (filename);
}
// Store a file
filename = "test.csv";
fh = fopen ("test.csv", "r");
ret = ftp_stor (conn, filename, fh);
fclose (fh);
if (ret===true) {
}
else {
print ("STOR failed. ", ret);
}
// List directory
dirList = array ();
ret = ftp_list (conn, dirList);
if (ret===true) {
print_r (dirList);
}
else {
print ("LIST failed. ", ret);
}
// Rename a file
ret = ftp_rnfr (conn, filename);
if (ret===true) {
newFilename = filename.".new";
ret = ftp_rnto (conn, newFilename);
if (ret===true) {
filename = newFilename;
}
else {
print ("RNTO failed. ", ret);
}
}
else {
print ("RNFR failed. ", ret);
}
// Delete a file
ret = ftp_dele (conn, filename);
if (ret===true) {
}
else {
print ("DELE failed. ", ret);
}
// Create a directory
ret = ftp_mkd (conn, "test");
if (ret===true) {
}
else {
print ("MKD failed. ", ret);
}
// Remove directory
ret = ftp_rmd (conn, "test");
if (ret===true) {
}
else {
print ("RMD failed. ", ret);
}
// Quit ftp connection
ftp_quit (conn);
}
else {
print ("FTP connection failed. ", conn);
exit (1);
}
?>