Skip to content

Instantly share code, notes, and snippets.

@jctoledo
Last active December 23, 2015 04:39
Show Gist options
  • Save jctoledo/6581779 to your computer and use it in GitHub Desktop.
Save jctoledo/6581779 to your computer and use it in GitHub Desktop.
Get a list of FTP files given a path and an extension
<?php
$list = getFtpFileList('ftp.ebi.ac.uk','/pub/databases/chembl/ChEMBL-RDF/16.0/', 'ttl.gz');
$total = count($list);
print_r($list);
/**
* Given an FTP uri get a non recursive list of all files of a given extension
* located inside a given path
*/
function getFtpFileList($ftp_uri, $path, $extension){
$rm = array();
// set up basic connection
$conn_id = ftp_connect($ftp_uri);
$ftp_user = 'anonymous';
if (@ftp_login($conn_id, $ftp_user, '')) {
} else {
echo "Couldn't connect as $ftp_user\n";
exit;
}
// get contents of the current directory
$contents = ftp_nlist($conn_id, $path);
foreach($contents as $aFile){
$reg_exp = "/.*\/(.*".$extension.")/";
preg_match($reg_exp, $aFile, $matches);
if(count($matches)){
$rm[] = $matches[1];
}
}
return $rm;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment