Getting the list of files over FTP


Hi Guys,
i was searching on net , for getting list of files over FTP connection for Bloomberg.
I have found some good code and i have modified a little and sharing with you.

This will fetch , Files and Folder on FTP location and list them.

<html>
<head>
<style type="text/css">
.file{
font-size:10px;
color:green;
}
.folder{
font-size:14px;
color:blue;
}
</style>
</head>
<body>
<?php
$ftp_server = "your.ftp.com";
$ftp_user = "user";
$ftp_password = "pass";

/* connect */
$ftp_connection = @ftp_connect($ftp_server);
if (!$ftp_connection) die('could not connect.');

/* login */
$ftp_login = @ftp_login($ftp_connection, $ftp_user, $ftp_password);
if (!$ftp_login) die('could not login.');

/* enter passive mode */
$ftp_passive = @ftp_pasv($ftp_connection, true);
if (!$ftp_passive) die('could not enable passive mode.');

/* get listing of files*/
/*
$ftp_listing = ftp_nlist($ftp_connection, ".");
foreach ($ftp_listing as $file){
echo "<div class='file'>".$file."</div>";
}
*/

/* get listing of folders*/

//identify directories
function ftp_is_dir($dir) {
global $ftp_connection;
if (@ftp_chdir($ftp_connection, $dir)) {
ftp_chdir($ftp_connection, '..');
return true;
}
else {
return false;
}
}
//$ftp_nlist = ftp_rawlist($ftp_connection, ".");
$ftp_nlist = ftp_nlist($ftp_connection, ".");
//alphabetical sorting
sort($ftp_nlist);

foreach ($ftp_nlist as $v) {
//1. ftp_is_dir() is true => directory
if (ftp_is_dir($v)) {
//output as [ directory ]
echo "<div class='folder'>".$v."</div>";
}
else{
echo "<div class='file'>".$v."</div>";
}
}
ftp_close($ftp_connection);
?>
</body>
</html>

Share on Facebook




About Pragnesh Karia

Pragnesh Karia, Open Source Enthusiast, Software Professional, Software Developer, Technical Lead ,Magento, Joomla ,Joomla LMS , Moodel LMS ,PHP ,Mysql, Ajax, Javascript, Jquery, Linux, Fan of Open Sources , Annet Technologies , SEO Analyst , Mootools