MaggsWeb:7

www.maggsweb.com


Taken from http://mypaaji.com/Index.php/how-to-download-upload-transfer-files-over-ftp/


The below given listing illustrates the process. First, a connection to the FTP server is initialized with a call to ftp_connect(); this function returns a connection handle that is used in all subsequent calls. The ftp_login() function is then used to log in to the server, while the ftp_close() function is used to terminate the connection and end the session. Between the calls to ftp_connect() and ftp_disconnect(), it is possible to perform all the actions commonly associated with an FTP session.

The below given listing illustrates four of the most common: listing files using ftp_nlist(); uploading files with ftp_put(); downloading files with ftp_get(); and deleting files with ftp_delete(). Note that ftp_put() and ftp_get() must be supplied with the remote and local file name, together with the transfer type (binary or ASCII).

<?php
// set access parameters
$host = "ftp.domain.dom";
$user = "user";
$pass = "password";
$dir = "/htdocs";
 
// open connection to FTP server
$conn = ftp_connect($host) or die("ERROR: Cannot connect");
 
// log in
ftp_login($conn, $user, $pass) or die("ERROR: Cannot log in");
 
// get file listing
$list = ftp_nlist($conn, ".") or die("ERROR: Cannot list files");
foreach ($list as $file) {
	echo "$file";
}
 
// Download a file
$remote = "code.zip";
$local = "code_sny.zip";
ftp_get($conn, $local, $remote, FTP_BINARY) or die("ERROR: Cannot download file: $remote");
echo "File [$remote] successfully downloaded as [$local]";
 
// Upload a file
$local = "photoSny.jpg";
$remote = "photoSny.jpg";
ftp_put($conn, $remote, $local, FTP_BINARY) or die("ERROR: Cannot upload file: $local");
echo "File [$local] successfully uploaded as [$remote]";
 
// Delete a file
$remote = "someFile.tmp";
ftp_delete($conn, $remote) or die("ERROR: Cannot delete file: $remote");
echo "File [$remote] successfully deleted";
 
// Disconnect
ftp_close($conn);
?>

Write a Comment

Let me know what you think?