About

eXTReMe Tracker

How To Make FTP Connection in PHP

How To Make FTP Connection in PHP

Introduction

The methods in this article implement client access to file servers speaking the File Transfer Protocol (FTP). It mainly focuses on detailed access to file servers along with ample controls to handle files on them.



A FTP connection requires certain parameters to successfully establish a connection. They are: Host name, username (with permissions to access the hosted files) and the password to validate the connection.

Basic Example of FTP connection 

<?php
 
$ftp_server = '192.168.1.1';
$ftp_user_name = 'anonymous';
$ftp_password = 'ftpconnect';
$connect = @ftp_connect($ftp_server);// Connection to the server
$login = @ftp_login($connect, $ftp_user_name, $ftp_password);// Validate login
if(!$connect) echo "Could not connect to the server!<br>";
else echo "Connection Established!<br>";
if(!$login) echo "Login failed!<br>";
else echo "Login Successful!<br>";
 
$upload = @ftp_put($connect, $destination_file, $source_file, FTP_BINARY);// Upload File
 
if(!$upload) echo "Could not upload the file";
else echo "File uploaded!";
@ftp_close($connect);// Connection closed
?> 

 
Untitled.png

Connecting to the Server through specific PORT 

$connect = @ftp_connect($ftp_server, '22'); // Connection to the server
  

Listing contents of a directory on File Server

<?php


$ftp_server = '192.168.1.1';
$ftp_user_name = 'anonymous';
$ftp_password = '';
$connect = @ftp_connect($ftp_server);// Connection to the server
$login = @ftp_login($connect, $ftp_user_name, $ftp_password); // Validate login
if(!$connect) echo "Could not connect to the server!<br>";
else echo "Connection Established!<br>";
if(!$login) echo "Login failed!<br>";
else echo "Login Successful!<br>";
$contents = @ftp_nlist($connect, './pub'); // List Files
echo "<br>";
foreach($contents as $file)
{
echo $file."<br>";
}
@ftp_close($connect); // Connection closed
?>
list.png
How To Make FTP Connection in PHP How To Make FTP Connection in PHP Reviewed by Unknown on 01:26 Rating: 5

No comments:

Powered by Blogger.