PHP and FTP
Introductions –
PHP provides FTP library to connect to file server using File Transfer Protocol. No external libraries are needed to build this extension. In order to use FTP functions with your PHP configuration, you should add the –enable-ftp or –with-ftp option when installing PHP 5 and other versions. The windows version of PHP has built in support for this extension. You do not need to load any additional extension in order to use these functions…
** Once the installation is done you can check by executing the below script –
URL – http://localhost/phpinfo.php
<?php
phpinfo(); // show the information of php.ini
?>
Using PHP we can make web interface which will do the entire file upload, download and execute shell commands on remote server. We can make Web FTP client using PHP FTP library.
examples –
How to upload file using PHP-FTP function –
<?php
// set up basic connection
$conn_id = ftp_connect($ftp_server);
// login with username and password
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
// check connection
if ((!$conn_id) || (!$login_result)) {
echo “FTP connection has failed!”;
echo “Attempted to connect to $ftp_server for user $ftp_user_name”;
exit;
} else {
echo “Connected to $ftp_server, for user $ftp_user_name”;
}
// upload the file
$upload = ftp_put($conn_id, $destination_file, $source_file, FTP_BINARY);
// check upload status
if (!$upload) {
echo “FTP upload has failed!”;
} else {
echo “Uploaded $source_file to $ftp_server as $destination_file”;
}
// close the FTP stream
ftp_close($conn_id);
?>
Thank you,
Santhosh Tirunahari