Last active
December 23, 2015 04:39
-
-
Save jctoledo/6581779 to your computer and use it in GitHub Desktop.
Get a list of FTP files given a path and an extension
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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