FTP: Getting a Directory Listing


We'll start our FTP work by using the ftp_nlist function to get a listing of a remote directory; here's how you use this function:

 array ftp_nlist(resource ftp_stream, string directory) 

This function returns an array of filenames from the specified directory on success or FALSE on error. In this case, we'll connect to an FTP server and get a listing of the directory code22 on that server. Here's how we connect to the FTP server:

 <?php     $connect = ftp_connect("ftp.ispname.com");     $result = ftp_login($connect, "username", "password");         .         .         . ?> 

We get the listing of the files in the code22 directory this way:

 <?php     $connect = ftp_connect("ftp.ispname.com");     $result = ftp_login($connect, "username", "password");     $a = ftp_nlist($connect, "code22");         .         .         . ?> 

And we'll display the files in that directory this way:

 <?php     $connect = ftp_connect("ftp.ispname.com");     $result = ftp_login($connect, "username", "password");     $a = ftp_nlist($connect, "code22");     foreach($a as $value){         echo $value, "<BR>";     } ?> 

All this is put to work in phpftp.php, Example 9-4.

Example 9-4. Getting a remote directory listing, phpftp.php
 <HTML>     <HEAD>         <TITLE>             Getting a directory listing with FTP         </TITLE>     </HEAD>     <BODY>         <CENTER>             <H1>                 Getting a directory listing with FTP             </H1>             Here's what's in the remote directory:             <BR>             <BR>             <?php                 $connect = ftp_connect("ftp.ispname.com");                 $result = ftp_login($connect, "username", "password");                 $a = ftp_nlist($connect, "code22");                 foreach($a as $value){                     echo $value, "<BR>";                 }             ?>          </CENTER>     <BODY> </HTML> 

You can see the results in Figure 9-5, where we've successfully retrieved a directory listing of the remote directory.

Figure 9-5. Getting a remote directory listing.




    Spring Into PHP 5
    Spring Into PHP 5
    ISBN: 0131498622
    EAN: 2147483647
    Year: 2006
    Pages: 254

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net