This was posted on the php.net site.
PHP Code:
On my PC (XP and Apache installed) - ftp_rawlist (with Parameter true) does only print a folder list - no subfolders, no files.
So i created this recursive function that writes all filenames (incl. paths) to a array. I tested it on Mac, Linux and Windows - it works (as long as you don't use folders with spaces...).
If you need more information: feel free to set more split[x]-Options and write them to the $files-array.
<?php
# the directory where ftp_rawlist starts
$startdir = "example";
# optional Datatypefilter (leave blank if not needed)
$suffix = "gif,png,jpeg,pdf,php";
# ftp-login
$ftp_server = "";
$ftp_user = "";
$ftp_pw = "";
$ftp_mode = "";
$conn_id = ftp_connect($ftp_server);
ftp_login($conn_id, $ftp_user, $ftp_pw) OR die("<br>ftp-login failed");
ftp_pasv($conn_id, true);
#*********************************************************************
# create filelist (recursiv)
#*********************************************************************
$files = array(); # must be defined here
$files = raw_list("$startdir");
#*********************************************************************
# print result
#*********************************************************************
$i = 0; $count = count($files);
while ($i < $count):
echo "$files[$i]<br>";
$i++;
endwhile;
ftp_close($conn_id);
#*********************************************************************
# rawlist in recursive form (without parameter true!!!)
#*********************************************************************
function raw_list($folder)
{
Global $conn_id;
Global $suffix;
Global $files;
$suffixes = explode(",", $suffix);
$list = ftp_rawlist($conn_id, $folder);
$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 = "$folder/$ItemName";
if (substr($list[$i],0,1) === "d" AND substr($ItemName,0,1) != "."):
# array_push($files, $path); # write directory in array if desired
raw_list($path);
elseif (substr($ItemName,0,2) != "._" AND in_array($endung,$suffixes)):
array_push($files, $path);
endif;
$i++;
endwhile;
return $files;
}
?>
Ueli, Zurich
Bookmarks