Simple FTP upload: with PHP and Perl

3 11 2006

Three simple code snippets for uploading files to a FTP server with PHP and Perl. I hope these three (might add more later) code entries will help someone find way in the forest. Here the goal is to upload some files on a ftp server quicly and easily.

There are two PHP versions here, one uses basic ftp methods and other does it with curl. In PHP arena curl is by far the best way to go about online file transfer, using any protocol. In Perl however, there are range of smart CPAN modules to choose from, each specializing on specific area (LWP::UserAgent, WWW::Mechanize etc.).

The Perl one uses CPAN module Net::FTP::Simple, instead of low level Net::FTP, which deals with FTP commands directly (Net::FTP::Simple uses Net::FTP behind the curtain).

PHP version1: using basic ftp methods
$ftp_server = 'your.ftpserver.com';
$ftp_user_name = 'user';
$ftp_user_pass = 'password';
$destination_file = '/ftpdir/myfile.txt';
$source_file = 'myfile.txt';

// Open FTP 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 the 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 connection
ftp_close($conn_id);

PHP version2: using curl
$ftp_server = 'your.ftpserver.com';
$ftp_user_name = 'user';
$ftp_user_pass = 'password';
$destination_file = '/ftpdir/myfile.txt';
$source_file = 'myfile.txt';

$ch = curl_init();
$fp = fopen ($source_file, "r");

// we upload a text file
curl_setopt($ch, CURLOPT_URL,
"ftp://" . $ftp_user_name . ":" . $ftp_user_pass . "@" . $ftp_server . $destination_file);
curl_setopt($ch, CURLOPT_UPLOAD, 1);
curl_setopt($ch, CURLOPT_INFILE, $fp);

// set size of the file, which isn't _mandatory_ but helps libcurl to do
// extra error checking on the upload.
curl_setopt($ch, CURLOPT_INFILESIZE, filesize($source_file));

$error = curl_exec ($ch);
if ($error){echo "Error uploading file\n";}

curl_close ($ch);

Perl version: using Net::FTP::Simple
use strict;
use warnings;
use Net::FTP::Simple;
# Net::FTP::Simple wraps basic ftp tasks in easy to use functions

my $username = 'user';
my $password = 'password';
my $server = 'your.ftpserver.com';

# connect, check connection, upload files and report success or failure (if debug_ftp is true then prints the steps)
my @sent_files = Net::FTP::Simple->send_files({
username => $username,
password => $password,
server => $server,
remote_dir => '/ftpdir/',
debug_ftp => 1,
files => [ 'myfile1.txt','myfile2.xml','myfile2.csv', ],
});

# OPTIONAL: connect, check connection, get list of files and report success or failure (if debug_ftp is true then prints the steps)
my @remote_files = Net::FTP::Simple->list_files({
username => $username,
password => $password,
server => $server,
remote_dir => '/ftpdir/',
debug_ftp => 1,
});
# OPTIONAL: print list of files in ftp dir
print ("List:\n\t", join("\n\t", @remote_files), "\n") if @remote_files;
exit 0;


Actions

Information

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s




%d bloggers like this: