Retrieving FTP File List

I want to print a list of files and only files from an FTP server, thatโ€™s what I was able to accomplish.

<?php $ftp_server = "my ftp server"; $conn_id = ftp_connect($ftp_server); $ftp_user_name = "ftp username"; $ftp_user_pass = "ftp password"; $login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass); $contents = ftp_nlist($conn_id, '/'); for ($i = 0 ; $i < count($contents) ; $i++) echo "<li>" . substr($contents[$i],1) . "</li>"; ftp_close($conn_id); ?> 

but it prints the names of files and folders. How can I just print the file names (files may not have extensions!)

+7
php ftp
source share
5 answers

Options:

1) you can use ftp_rawlist instead of ftp_nlist to get a complete list for the file / directory, which should indicate if it is a directory. However, the format of this list will depend on the ftp server operating system. For example, on a unix / linux system, the source list might look something like this:

 drwxrwxr-x 3 jm72 jm72 4096 Nov 2 16:39 myDir -rw-rw-r-- 1 jm72 jm72 257 Nov 2 16:39 myFile 

where the "d" in the first column will tell you the directory. Not sure how it will look on a Windows server.

2) for each returned file name, try writing it to a CD. If you can, this is a directory!

 if (ftp_chdir($conn_id, substr($contents[$i],1)) { //it a directory, don't include it in your list ftp_cdup($conn_id) //don't forget to go back up to the directory you started in! } 
+5
source share

Here is a script that will do this for you, a poster on ftp_nlist (PHP Docs) is kindly provided:

 <?php //identify directories function ftp_is_dir($dir) { global $ftp_connect; if (@ftp_chdir($ftp_connect, $dir)) { ftp_chdir($ftp_connect, '..'); return true; } else { return false; } } $ftp_nlist = ftp_nlist($ftp_connect, "."); //alphabetical sorting sort($ftp_nlist); foreach ($ftp_nlist as $v) { //1. ftp_is_dir() is true => directory if (ftp_is_dir($v)) { //output as [ directory ] echo "[ " . $v . " ]<br />\n"; } } foreach ($ftp_nlist as $v) { //2. ftp_is_dir() is false => file if (!ftp_is_dir($v)) { //output as file echo "" . $v . "<br />\n"; } } ?> 
+3
source share

if you use ftp_rawlist as follows:

 $rawfiles = ftp_rawlist($conn, true); //true being for recursive foreach ($rawfiles as $rawfile) { $info = preg_split("/[\s]+/", $rawfile, 9); if($info[0]{0} == 'd') { //Directory }else { //File $size = byteconvert($info[4]); $chmod = chmodnum($info[0]); $date = strtotime($info[6] . ' ' . $info[5] . ' ' . $info[7]); } } 

Get closer to your goal.

0
source share

You can also use the ftp_mdtm() function. This always returns an error (-1) when it fails, which indicates a directory, since this function does not work for directories. One caveat is that it does not work on some systems (the manual does not indicate which ones), so you will need to check if it worked in the first place.

0
source share

If you are on the same server, you can do:

 $contents = ftp_nlist($conn_id, '/'); // set the path to the folder $path = '/home/user/public_html/my_folder/'; foreach ( $contents as $item ) { if ( is_file( $path . $item ) ) { echo $item . PHP_EOL; } else { continue; } } 

For a remote server, you can check if the file exists in other ways .

0
source share

All Articles