note 86780 deleted from function.ftp-rawlist by crell

From: Date: Thu, 31 Mar 2022 19:18:07 +0000
Subject: note 86780 deleted from function.ftp-rawlist by crell
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-217895@lists.php.net to get a copy of this message
Note Submitter: phpnet uschlaepfer ch ---- 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

« previous php.notes (#217895) next »