Ruzzas Posted June 30, 2010 Share Posted June 30, 2010 I need a function which gets every file in the FTP such as: ./folder/folder2/file.php ./folder2/folder3/file3.php ./file.php ./test/this.php Quote Link to comment https://forums.phpfreaks.com/topic/206292-ftp-listing-directory-and-files-inside-php/ Share on other sites More sharing options...
premiso Posted June 30, 2010 Share Posted June 30, 2010 ftp Lots of functions there! Quote Link to comment https://forums.phpfreaks.com/topic/206292-ftp-listing-directory-and-files-inside-php/#findComment-1079204 Share on other sites More sharing options...
Adam Posted June 30, 2010 Share Posted June 30, 2010 "every file in the FTP"? Can't fully understand what you're asking here. Quote Link to comment https://forums.phpfreaks.com/topic/206292-ftp-listing-directory-and-files-inside-php/#findComment-1079250 Share on other sites More sharing options...
Ruzzas Posted July 1, 2010 Author Share Posted July 1, 2010 "every file in the FTP"? Can't fully understand what you're asking here. I've got it connecting to the ftp server. Basically I just want to list every file in the ftp connection such as: ./file.php ./folder/file.php ./emptyfolder ./folder/file.php ./folder/emptyfolder Quote Link to comment https://forums.phpfreaks.com/topic/206292-ftp-listing-directory-and-files-inside-php/#findComment-1079483 Share on other sites More sharing options...
premiso Posted July 1, 2010 Share Posted July 1, 2010 ftp Lots of functions there! You do realize that RTFM can help you immensely, especially the user comments. I didn't just post that link for you to ignore it. http://www.php.net/manual/en/function.ftp-rawlist.php#86780 Is something along the lines of what you want, a recursive directory list. If that is not what you want look at modifying it or look through other user comments to see if someone setup something you want. You may also want to check at ftp_nlist and look at those comments as well. Quote Link to comment https://forums.phpfreaks.com/topic/206292-ftp-listing-directory-and-files-inside-php/#findComment-1079499 Share on other sites More sharing options...
Ruzzas Posted July 1, 2010 Author Share Posted July 1, 2010 ftp_nlist only lists the files and folders in that directory its not recursive supported. Quote Link to comment https://forums.phpfreaks.com/topic/206292-ftp-listing-directory-and-files-inside-php/#findComment-1079524 Share on other sites More sharing options...
Ruzzas Posted July 1, 2010 Author Share Posted July 1, 2010 ftp_nlist only lists the files and folders in that directory its not recursive supported. Rawlist doesn't list it either so... yeah Quote Link to comment https://forums.phpfreaks.com/topic/206292-ftp-listing-directory-and-files-inside-php/#findComment-1079527 Share on other sites More sharing options...
ChemicalBliss Posted July 1, 2010 Share Posted July 1, 2010 The link premiso gave you takes you to a user comment that has a script posted by the author that does exactly what you have asked for. (There are other recursive scripts posted in the user comments for that page). -cb- Quote Link to comment https://forums.phpfreaks.com/topic/206292-ftp-listing-directory-and-files-inside-php/#findComment-1079537 Share on other sites More sharing options...
Ruzzas Posted July 1, 2010 Author Share Posted July 1, 2010 The link premiso gave you takes you to a user comment that has a script posted by the author that does exactly what you have asked for. (There are other recursive scripts posted in the user comments for that page). -cb- Its not what Im looking for... I want a simple little ftp_nlist i'm working on which shows directorys and the files inside (Recursive) I can do it fine with local, But FTP is the worst one out of them all. Ftp_rawlist with the recursive turned to true still does not show what the folders contain in them, so generally it fails. Quote Link to comment https://forums.phpfreaks.com/topic/206292-ftp-listing-directory-and-files-inside-php/#findComment-1079540 Share on other sites More sharing options...
Ruzzas Posted July 1, 2010 Author Share Posted July 1, 2010 Here is my remote listing function: function RemoteListDirectory($Directory, $Recursive){ global $Connection; if($Connection){ if(ftp_chdir($Connection, $Directory)){ if($BaseArray = ftp_nlist($Connection, ".")){ for($I=0;$I<count($BaseArray);$I++){ if($BaseArray[$I] != ""){ ## If filename is not empty $FileSize = ftp_size($Connection, $BaseArray[$I]); if($FileSize == -1){ echo "It thinks $BaseArray[$I] is a directory..."; print_r($FileSize); echo "Changing directory to: $BaseArray[$I]"; $Array = array_merge($Array, RemoteListDirectory($BaseArray[$I], True)); @ftp_cdup($Connection); }else{ if($Recursive){ $File = "$Directory/$BaseArray[$I]"; $Array[] = preg_replace("/\/\//si", "/",$BaseArray[$I]); }else{ $Array[] = preg_replace("/\/\//si", "/", $BaseArray[$I]); } } }else{ echo "It thinks $BaseArray[$I] is empty..."; } } } } }else{ return False; } $Array = array_values($Array); return $Array; } Could someone help me with this, it comes out like this: It thinks ./graphs is a directory...-1Changing directory to: ./graphsIt thinks ./soundcache is a directory...-1Changing directory to: ./soundcacheArray ( [0] => ./gm_construct.bsp [1] => ./gm_flatgrass.bsp [2] => ./gm_construct.ain [3] => ./gm_flatgrass.ain [4] => ./sb_forlorn_sb3_r2l.ain [5] => ./sb_Forlorn_sb3_R2L.bsp [6] => ./sb_gooniverse.bsp [7] => ./sb_lostinspace_rc5.bsp [8] => ./sb_new_worlds_2.bsp [9] => ./sb_Spacewar_SB3_V1.bsp [10] => ./sb_twinsuns_fixed.bsp [11] => ./_other.cache [12] => ./_other_rebuild.cache [13] => ./_sharedprecache.cache ) some files are meant to be in folders. Quote Link to comment https://forums.phpfreaks.com/topic/206292-ftp-listing-directory-and-files-inside-php/#findComment-1079541 Share on other sites More sharing options...
ChemicalBliss Posted July 1, 2010 Share Posted July 1, 2010 Here try mine (just wrote it): <?php $ftp_server = "127.0.0.1"; $ftp_user_name = "test"; $ftp_user_pass = "pass"; // set up basic connection $conn_id = ftp_connect($ftp_server); // login with username and password $login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass); // Get current directory $current_directory = ftp_pwd($conn_id); // Recursive function function ftp_nlistr($resource, $thispath, $thelist=array(), $first=TRUE){ if(!ftp_is_dir($resource,$thispath)){ // for Files (anything that isnt a readable directory) if($first == TRUE){ return array("Path doesn't Exist (".$thispath.")"); } $thelist[] = $thispath; return $thelist; }else{ $contents = ftp_nlist($resource, $thispath); // For empty folders if(count($contents) == 0){ $thelist[] = $thispath; return $thelist; } // Recursive Part foreach($contents As $file){ $thelist = ftp_nlistr($resource, $file, $thelist,FALSE); } return $thelist; } } // Simple function, checks wether its a folder by trying to change the current directory to it, if it fails, returns to the top-level directory. function ftp_is_dir($conn, $dir) { $cdir = ftp_pwd($conn); if (@ftp_chdir($conn, $dir)) { ftp_chdir($conn, $cdir); return true; } else { return false; } } print_r(ftp_nlistr($conn_id, ".")); // Dont use a trailing slash in the directory your specifying to look through ?> -cb- Quote Link to comment https://forums.phpfreaks.com/topic/206292-ftp-listing-directory-and-files-inside-php/#findComment-1079545 Share on other sites More sharing options...
Ruzzas Posted July 1, 2010 Author Share Posted July 1, 2010 you can just check if the filesize is -1 to check if its a folder or not Quote Link to comment https://forums.phpfreaks.com/topic/206292-ftp-listing-directory-and-files-inside-php/#findComment-1079551 Share on other sites More sharing options...
Ruzzas Posted July 1, 2010 Author Share Posted July 1, 2010 Now I have it as: It thinks ./graphs is a directory...-1Changing directory to: ./graphsIt thinks ./soundcache is a directory...-1Changing directory to: ./soundcacheArray ( [0] => ./gm_construct.bsp [1] => ./gm_flatgrass.bsp [2] => ./graphs/./gm_construct.ain [3] => ./graphs/./gm_flatgrass.ain [4] => ./graphs/./sb_forlorn_sb3_r2l.ain [5] => ./sb_Forlorn_sb3_R2L.bsp [6] => ./sb_gooniverse.bsp [7] => ./sb_lostinspace_rc5.bsp [8] => ./sb_new_worlds_2.bsp [9] => ./sb_Spacewar_SB3_V1.bsp [10] => ./sb_twinsuns_fixed.bsp [11] => ./soundcache/./_other.cache [12] => ./soundcache/./_other_rebuild.cache [13] => ./soundcache/./_sharedprecache.cache ) Quote Link to comment https://forums.phpfreaks.com/topic/206292-ftp-listing-directory-and-files-inside-php/#findComment-1079563 Share on other sites More sharing options...
Ruzzas Posted July 1, 2010 Author Share Posted July 1, 2010 Here try mine (just wrote it): <?php $ftp_server = "127.0.0.1"; $ftp_user_name = "test"; $ftp_user_pass = "pass"; // set up basic connection $conn_id = ftp_connect($ftp_server); // login with username and password $login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass); // Get current directory $current_directory = ftp_pwd($conn_id); // Recursive function function ftp_nlistr($resource, $thispath, $thelist=array(), $first=TRUE){ if(!ftp_is_dir($resource,$thispath)){ // for Files (anything that isnt a readable directory) if($first == TRUE){ return array("Path doesn't Exist (".$thispath.")"); } $thelist[] = $thispath; return $thelist; }else{ $contents = ftp_nlist($resource, $thispath); // For empty folders if(count($contents) == 0){ $thelist[] = $thispath; return $thelist; } // Recursive Part foreach($contents As $file){ $thelist = ftp_nlistr($resource, $file, $thelist,FALSE); } return $thelist; } } // Simple function, checks wether its a folder by trying to change the current directory to it, if it fails, returns to the top-level directory. function ftp_is_dir($conn, $dir) { $cdir = ftp_pwd($conn); if (@ftp_chdir($conn, $dir)) { ftp_chdir($conn, $cdir); return true; } else { return false; } } print_r(ftp_nlistr($conn_id, ".")); // Dont use a trailing slash in the directory your specifying to look through ?> -cb- That worked, I altered it to my liking and now it is: function RemoteListDirectory($Directory, $Array = array(), $First = True){ global $Connection; if($Connection){ if($First){ ftp_chdir($Connection, $Directory); } if(ftp_size($Connection, $Directory) != -1){ if($First){ echo "Folder does not exist."; return False; } $Array[] = $Directory; return $Array; }else{ $Contents = ftp_nlist($Connection, $Directory); if(!count($Contents)){ $Array[] = $Directory; return $Array; } foreach($Contents as $File){ $Array = RemoteListDirectory($File, $Array, False); } $Array = array_values($Array); return $Array; } }else{ return False; } } The problem is, I get the whole Directory of were the files are located: Remote Directory: Array ( [0] => /NMD-Spacebuild/orangebox/garrysmod/maps/gm_construct.bsp [1] => /NMD-Spacebuild/orangebox/garrysmod/maps/gm_flatgrass.bsp [2] => /NMD-Spacebuild/orangebox/garrysmod/maps/graphs/gm_construct.ain [3] => /NMD-Spacebuild/orangebox/garrysmod/maps/graphs/gm_flatgrass.ain [4] => /NMD-Spacebuild/orangebox/garrysmod/maps/graphs/sb_forlorn_sb3_r2l.ain [5] => /NMD-Spacebuild/orangebox/garrysmod/maps/sb_Forlorn_sb3_R2L.bsp [6] => /NMD-Spacebuild/orangebox/garrysmod/maps/sb_gooniverse.bsp [7] => /NMD-Spacebuild/orangebox/garrysmod/maps/sb_lostinspace_rc5.bsp [8] => /NMD-Spacebuild/orangebox/garrysmod/maps/sb_new_worlds_2.bsp [9] => /NMD-Spacebuild/orangebox/garrysmod/maps/sb_Spacewar_SB3_V1.bsp [10] => /NMD-Spacebuild/orangebox/garrysmod/maps/sb_twinsuns_fixed.bsp [11] => /NMD-Spacebuild/orangebox/garrysmod/maps/soundcache/_other.cache [12] => /NMD-Spacebuild/orangebox/garrysmod/maps/soundcache/_other_rebuild.cache [13] => /NMD-Spacebuild/orangebox/garrysmod/maps/soundcache/_sharedprecache.cache ) Quote Link to comment https://forums.phpfreaks.com/topic/206292-ftp-listing-directory-and-files-inside-php/#findComment-1079596 Share on other sites More sharing options...
Ruzzas Posted July 1, 2010 Author Share Posted July 1, 2010 Alright I've got the bug sorted above this post, I'm having some difficulty fixing this part: function Download($Array){ global $Connection; if($Connection){ for($I=0;$I<count($Array);$I++){ if($ArrayExplode = (explode(".", $Array[$I]))){ if($ArrayExplode[1] != "dua" & $ArrayExplode[1] != "txt"){ Zip($Array[$I]); } } if($Array[$I] != ""){ if(ftp_size($Connection,$Array[$I]) != -1){ ftp_get($Connection,$Array[$I],$Array[$I],FTP_BINARY); } } } return True; }else{ return False; } } It doesn't download folders (mkdir) them and then download the folders crap <br /> <b>Warning</b>: ftp_get(./graphs/gm_construct.ain) [<a href='function.ftp-get'>function.ftp-get</a>]: failed to open stream: No such file or directory in <b>/home4/nmdgamin/public_html/gmod1/fastdl/functions.php</b> on line <b>161</b><br /> <br /> <b>Warning</b>: ftp_get() [<a href='function.ftp-get'>function.ftp-get</a>]: Error opening ./graphs/gm_construct.ain in <b>/home4/nmdgamin/public_html/gmod1/fastdl/functions.php</b> on line <b>161</b><br /> <br /> <b>Warning</b>: ftp_get(./graphs/gm_flatgrass.ain) [<a href='function.ftp-get'>function.ftp-get</a>]: failed to open stream: No such file or directory in <b>/home4/nmdgamin/public_html/gmod1/fastdl/functions.php</b> on line <b>161</b><br /> <br /> <b>Warning</b>: ftp_get() [<a href='function.ftp-get'>function.ftp-get</a>]: Error opening ./graphs/gm_flatgrass.ain in <b>/home4/nmdgamin/public_html/gmod1/fastdl/functions.php</b> on line <b>161</b><br /> <br /> <b>Warning</b>: ftp_get(./graphs/sb_forlorn_sb3_r2l.ain) [<a href='function.ftp-get'>function.ftp-get</a>]: failed to open stream: No such file or directory in <b>/home4/nmdgamin/public_html/gmod1/fastdl/functions.php</b> on line <b>161</b><br /> <br /> <b>Warning</b>: ftp_get() [<a href='function.ftp-get'>function.ftp-get</a>]: Error opening ./graphs/sb_forlorn_sb3_r2l.ain in <b>/home4/nmdgamin/public_html/gmod1/fastdl/functions.php</b> on line <b>161</b><br /> <br /> <b>Warning</b>: ftp_get(./soundcache/_other.cache) [<a href='function.ftp-get'>function.ftp-get</a>]: failed to open stream: No such file or directory in <b>/home4/nmdgamin/public_html/gmod1/fastdl/functions.php</b> on line <b>161</b><br /> <br /> <b>Warning</b>: ftp_get() [<a href='function.ftp-get'>function.ftp-get</a>]: Error opening ./soundcache/_other.cache in <b>/home4/nmdgamin/public_html/gmod1/fastdl/functions.php</b> on line <b>161</b><br /> <br /> Quote Link to comment https://forums.phpfreaks.com/topic/206292-ftp-listing-directory-and-files-inside-php/#findComment-1079609 Share on other sites More sharing options...
Ruzzas Posted July 1, 2010 Author Share Posted July 1, 2010 Could someone help me with this bit, I have no clue how to do this: Make the downloader checker explode with forward slashes => Make it loop (until theres no more folders left) => If folder doesn't exist in local then mkdir it => Get the contents of them folders from the ftp server and download it => Continue on with the rest of the script The Downloader is above. Quote Link to comment https://forums.phpfreaks.com/topic/206292-ftp-listing-directory-and-files-inside-php/#findComment-1079626 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.