FTP PHP

The functions in this section allow you to make connections to FTP servers. FTP is the file transfer protocol. While the file functions allow you to open and manipulate remote files by specifying a URL instead of a local path, these functions operate directly with the FTP protocol. They offer a greater degree of control. They also allow you to get a list of files on the server. The FTP functions were added to PHP by Andrew Skalski.M. boolean ftp_cdup(integer link) The ftp_cdup function changes the working directory to the parent directory. boolean ftp_chdir(integer link, string directory) The ftp_chdir function moves the working directory to the specified directory. integer ftp_connect(string host, integer port) Use ftp_connect to begin an FTP connection. The port argument is optional. An FTP reso

The functions in this section allow you to make connections to FTP servers. FTP is the file transfer protocol. While the file functions allow you to open and manipulate remote files by specifying a URL instead of a local path, these functions operate directly with the FTP protocol. They offer a greater degree of control. They also allow you to get a list of files on the server. The FTP functions were added to PHP by Andrew Skalski.M.

boolean ftp_cdup(integer link)
The ftp_cdup function changes the working directory to the parent directory.

boolean ftp_chdir(integer link, string directory)
The ftp_chdir function moves the working directory to the specified directory.

integer ftp_connect(string host, integer port)
Use ftp_connect to begin an FTP connection. The port argument is optional. An FTP resource identifier will be returned if the connection is successful, FALSE otherwise. This D is used in the rest of the FTP commands. Remem- ber that once you connect, you must log in before you can issue any commands.

<?
//connect to server
if(!($ftp = ftp_connect("localhost")))
{
print("Unable to connect!BR>\n");
exit();
}
print("Connected.BR>\n");
//log in
if(!ftp_login($ftp, "anonymous", "[email protected]"))
{
print("Unable to login!BR>\n");
exit();
}
print("Logged In.BR>\n");
//print system type
print("System Type: " . ftp_systype($ftp) . "BR>\n");
//make sure passive mode is off
ftp_pasv($ftp, FALSE);
//get working directory
print("Working Directory: " . ftp_pwd($ftp) . "BR>\n");
//get files in raw format
print("Raw List:BR>\n");
foreach(ftp_rawlist($ftp, ".") as $line)
{
print("$lineBR>\n");
}
print("BR>\n");
//move to pub directory
if(!ftp_chdir($ftp, "pub"))
{
print("Unable to go to the pub directory!BR>\n");
}
print("Moved to pub directory.BR>\n");
//get a list of files
print("Files:BR>\n");
foreach(ftp_nlist($ftp, ".") as $filename)
{
print("$filenameBR>\n");
}
print("BR>\n");
//return to root directory
if(!ftp_cdup($ftp))
{
print("Failed to move up a directory!BR>\n");
}
//close connection
ftp_quit($ftp);
?>

boolean ftp_delete(integer link, string path)
The ftp_delete function removes a file on the remote server. The link argument is as returned by ftp_connect. The path argument is the path on the remote server to the file to be deleted. See ftp_put for an example of use.

boolean ftp_fget(integer link, integer file, string filename, integer mode)
The ftp_fget function copies a remote file into an open file stream. You must create a file resource using fopen or a similar function to pass as the second argument. The mode argument should be set with one of two constants: FTP_ASCII or FTP_IMAGE. These are sometimes referred to as text or binary modes.

<?
//connect to server
if(!($ftp = ftp_connect("localhost")))
{
print("Unable to connect!BR>\n");
exit();
}
//log in
if(!ftp_login($ftp, "anonymous", "[email protected]"))
{
print("Unable to login!BR>\n");
exit();
}
//open local file for writing
$fp = fopen("/tmp/ftp_fget.test", "w");
//save remote file in open file stream
if(!ftp_fget($ftp, $fp, "data.txt", FTP_ASCII)))
{
print("Unable to get remote file!BR>\n");
}
//close local file
fclose($fp);
//close connection
ftp_quit($ftp);
?>

boolean ftp_fput(integer link, string remote, integer file, integer mode)
The ftp_fput function creates a file on the remote server from the contents of an open file stream. The link argument is as returned by ftp_connect. The remote argument is the path to the file to be created on the remote server. The file argument is a file identifier as returned by fopen or a similar function. The mode argument should be FTP_ASCII or FTP_IMAGE.

<?
//connect to server
if(!($ftp = ftp_connect("localhost"))
{
print("Unable to connect!BR>\n");
exit();
}
//log in
if(!ftp_login($ftp, "anonymous", "[email protected]"))
{
print("Unable to login!BR>\n");
exit();
}
//open local file
if(!($fp = fopen("/tmp/data.txt", "r"))
{
print("Unable to open local file!BR>\n");
exit();
}
//write file to remote server
ftp_fput($ftp, "data.txt", $fp, FTP_ASCII);
//close local file
fclose($fp);
//close connection
ftp_quit($ftp);
?>

boolean ftp_get(integer link, string local, string remote, integer mode)
Use ftp_get to copy a file from the remote server to local filesystem. The link argument is as returned by ftp_connect. The local and remote arguments specify paths. The mode argument should use FTP_ASCII or FTP_IMAGE.

<?
//connect to server
if(!($ftp = ftp_connect("localhost")))
{
print("Unable to connect!BR>\n");
exit();
}
//log in
if(!ftp_login($ftp, "anonymous", "[email protected]"))
{
print("Unable to login!BR>\n");
exit();
}
//save file to tmp directory
ftp_get($ftp, "/tmp/data.bin", "/pub/data.bin",FTP_IMAGE);
//close connection
ftp_quit($ftp);
?>

boolean ftp_login(integer link, string username, string password)
Once you make a connection to an FTP server, you must use ftp_login to identify yourself. All three arguments are required, even if you are logging in anonymously. See ftp_connect for an example of use.

integer ftp_mdtm(integer link, string path)
The ftp_mdtm function returns the last modification time for the file named in the path argument.

<?
//connect to server
if(!($ftp = ftp_connect("localhost")))
{
print("Unable to connect!BR>\n");
exit();
}
//log in
if(!ftp_login($ftp, "anonymous", "[email protected]"))
{
print("Unable to login!BR>\n");
exit();
}
//get the size of the README file
print("Size: " . ftp_size($ftp, "README") . "BR>\n");
//get the last modification date
print("Modified: " .
date("Y-m-d", ftp_mdtm($ftp, "README")) .
"BR>\n");
//close connection
ftp_quit($ftp);
?>

string ftp_mkdir(integer link, string directory)
The ftp_mkdir function creates a directory on the remote server. FALSE is returned if the directory cannot be created.

<?
//connect to server
if(!($ftp = ftp_connect("localhost")))
{
print("Unable to connect!BR>\n");
exit();
}
//log in
if(!ftp_login($ftp, "anonymous", "[email protected]"))
{
print("Unable to login!BR>\n");
exit();
}
//create a new directory
$result = ftp_mkdir($ftp, "corephp");
if($result)
{
print("Created directory: $resultBR>\n");
}
else
{
print("Unable to create corephp directory!BR>\n");
}
//remove corephp directory
if(!ftp_rmdir($ftp, "corephp"))
{
print("Unable to remove corephp directory!BR>\n");
}
//close connection
ftp_quit($ftp);
?>

array ftp_nlist(integer link, string directory)
The ftp_nlist function returns an array of files in the specified directory.

boolean ftp_pasv(integer link, boolean on)
Use ftp_pasv to turn passive mode on or off. It is off by default.

boolean ftp_put(integer link, string remote, string local, integer mode)
The ftp_put function copies a file from the local filesystem to the remote server. The link argument is as returned by ftp_connect. The local and remote arguments specify paths. The mode argument should be either FTP_ASCII or FTP_IMAGE.

<?
//connect to server
if(!($ftp = ftp_connect("localhost")))
{
print("Unable to connect!BR>\n");
exit();
}
//log in
if(!ftp_login($ftp, "anonymous", "[email protected]"))
{
print("Unable to login!BR>\n");
exit();
}
//copy local file to remote server
ftp_put($ftp, "/uploads/data.txt", "
/tmp/data.txt", FTP_ASCII);
//remove remote file
ftp_delete($ftp, "/uploads/data.txt");
//close connection
ftp_quit($ftp);
?>

string ftp_pwd(integer link)
The ftp_pwd function returns the name of the current directory. See ftp_connect for an example of use.

boolean ftp_quit(integer link)
Use ftp_quit to close an FTP connection.

array ftp_rawlist(integer link, string directory)
The ftp_rawlist returns the raw output of an ls -l command on the given directory.

boolean ftp_rename(integer link, string original, string new)
The ftp_rename function changes the name of a file on the remote server.

boolean ftp_rmdir(integer link, string directory)
Use ftp_rmdir to remove a directory.

integer ftp_size(integer link, string path)
The ftp_size function returns the size of a remote file in bytes. If an error occurs, -1 is returned.

string ftp_systype(integer link)
The ftp_systype function returns the system type of the remote FTP server.



Face Book Twitter Google Plus Instagram Youtube Linkedin Myspace Pinterest Soundcloud Wikipedia

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

PHP Topics