batch/framework/common/ftp.php
2015-07-20 08:17:28 +00:00

135 lines
4.8 KiB
PHP

<?php
function ftp_mget($ftp_url, $ftp_user, $ftp_pass, $ftp_dir, $local_dir, $debug=null) {
$conn_id = @ftp_connect($ftp_url);
if (!$conn_id) {
if ($debug<>null) echo date ('Y/m/d - H:i:s')." - ERREUR : Impossible de se connecter au serveur FTP ($ftp_url) !".EOL;
return false;
}
$login_result = @ftp_login($conn_id, $ftp_user, $ftp_pass);
if (!$login_result) {
if ($debug<>null) echo date ('Y/m/d - H:i:s')." - ERREUR : Impossible de s'authentifier sur le serveur FTP (user=$ftp_user)!".EOL;
return false;
}
// Récupération du contenu d'un dossier
$contents = ftp_nlist($conn_id, $ftp_dir);
// print_r($contents);
// print_r(ftp_rawlist($conn_id, $ftp_dir));
$nbFic=0; // Nombre de fichiers récupérés
if (is_array($contents))
foreach($contents as $k => $server_file) {
$tailleDist = ftp_size($conn_id, $server_file);
$dateDist = ftp_mdtm($conn_id, $server_file);
$tailleLoc=0;
if ($dateDist != -1) {
$tabFichiers[$server_file]['dateDispo']=date('Y-m-d H:i:s', $dateDist);
// echo date ('Y/m/d - H:i:s') ." - Le fichier distant $server_file a été modifié le ".date("d/m/Y à H:i:s.",$dateDist)."\n";
} else {
$tabFichiers[$server_file]['dateDispo']=NULL;
// echo date ('Y/m/d - H:i:s')." - ERREUR : Impossible de récupérer l'heure de modification du fichier distant $server_file !\n";
}
$tabFichiers[$server_file]['taille']=$tailleDist;
if ($tailleDist != -1) {
//echo date ('Y/m/d - H:i:s') ." - Taille du fichier distant $server_file est de $tailleDist octets\n";
}
if (file_exists($local_dir . $server_file)) {
$dateLoc=filemtime($local_dir . $server_file);
$tabFichiers[$server_file]['dateDownload']=date('Y-m-d H:i:s', $dateLoc);
$tailleLoc=filesize($local_dir . $server_file);
//echo date ('Y/m/d - H:i:s') ." - Taille du fichier local $server_file = $tailleLoc octets\n";
}
if ($tailleDist<>$tailleLoc) {
if (ftp_get($conn_id, $local_dir. $server_file, $server_file, FTP_BINARY))
//echo date ('Y/m/d - H:i:s')." - Fichier distant $server_file téléchargé avec succès".EOL;
$nbFic++;
else {
if ($debug<>null) echo date ('Y/m/d - H:i:s')." - ERREUR : Fichier distant $server_file non téléchargé !".EOL;
// return false;
}
}
}
// Fermeture de la connexion
ftp_close($conn_id);
return $nbFic;
}
/**
* ftpRecursiveFileListing
*
* Get a recursive listing of all files in all subfolders given an ftp handle and path
*
* @param resource $ftpConnection the ftp connection handle
* @param string $path the folder/directory path
* @return array $allFiles the list of files in the format: directory => $filename
*/
/*function ftp_rlist($conn_id, $path) {
static $allFiles = array();
$contents = ftp_nlist($conn_id, $path);
foreach($contents as $currentFile) {
// assuming its a folder if there's no dot in the name
echo $currentFile.EOL;
//if (strpos($currentFile, '.') === false) {
if(ftp_is_dir($conn_id, $currentFile)) {
ftp_rlist($conn_id, $path.'/'.$currentFile);
}
$allFiles[$path][] = substr($currentFile, strlen($path) + 1);
}
return $allFiles;
}
*/
//identify directories
function ftp_is_dir($conn_id, $dir) {
if (@ftp_chdir($conn_id, $dir)) {
ftp_chdir($conn_id, '../');
return true;
} else {
return false;
}
}
/*
function list_all_files($conn_id, $path){
$buff = ftp_rawlist($conn_id, $path);
$res = parse_rawlist( $buff) ;
static $flist = array();
if(count($res)>0){
foreach($res as $result){
// verify if is dir , if not add to the list of files
if($result['size']== 0){
// recursively call the function if this file is a folder
list_all_files($conn_id, $path.'/'.$result['name']);
}
else{
// this is a file, add to final list
$flist[] = $result;
}
}
}
return $flist;
}*/
function ftp_rlist($conn_id, $path, $suffixes=array('gif','png','jpeg','pdf','php','txt','csv'), $files=array()) {
ftp_pasv($conn_id, true);
// = explode(",", $suffix);
$list = ftp_rawlist($conn_id, $path);
$anzlist = count($list);
$i = 0;
while ($i < $anzlist):
$split = preg_split("/[\s]+/", $list[$i], 9, PREG_SPLIT_NO_EMPTY);
$ItemName = $split[8];
$endung = strtolower(substr(strrchr($ItemName,"."),1));
$path = "$path/$ItemName";
if (substr($list[$i],0,1) === "d" AND substr($ItemName,0,1) != "."):
// array_push($files, $path); # write directory in array if desired
ftp_rlist($conn_id,$path, $suffixes, $files);
elseif (substr($ItemName,0,2) != "._" AND in_array($endung,$suffixes)):
array_push($files, $path);
endif;
$i++;
endwhile;
return $files;
}
?>