Voir aussi

User Contributed Notes 35 notes

Some complain that ftp_nlist, always return FALSE. I did experience this behavior myself, until I used ftp_pasv, which is useful if your client is behind a firewall (which most clients are now), then ftp_nlist worked just fine. I don't really know what are all the implications of using ftp_pasv, but if you read or experience that ftp_nlist, ftp_get, ftp_nb_get doesn't work, try adding the following:

I noticed that this function won't work if working path is different, because $dir keeps value of whole path string. That's why adding basename()-function strips the path and this function will work also sub folders, not only in root folder.

php 5.2.2 on MAC OS 1.4 (Tiger) seems to defaut to active ftp. to get ftp_nlist functioning, i needed to force ftp to passive.the default behaviour with PHP 5.2.3 on windows XP seems to be passive ftp.

A derived use of ftp_nlist function: determine if a given path exists and is a directory or a plain file (this is not easy with basic ftp functions). This code doesn't issue any warning or error. It makes use of ls option -d for efficiency, and -F to append a slash if the retrieved path is a directory.

If we are giving second parameter as full directory path then it wil return the list of files with full path, If we want to list only files you can do as following code// 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);//if u want to display the files from /home/test///change directory point to /home/test usingftp_chdir($conn_id, "test");//then pass second parameter as null which will give the list of files.// get contents of the current directory$contents = ftp_nlist($conn_id, "");

Recursive directory delete using ftp_nlist() ...<?php function ftp_rmdirr($path, $handle) { if (!(@ftp_rmdir($handle, $path) || @ftp_delete($handle, $path))) {$list = ftp_nlist($handle, $path); if (!empty($list)) foreach($list as $value)ftp_rmdirr($value, $handle); } @ftp_rmdir($handle, $path); }?>It would be very useful if it was built into php. After all most of the time we want to remove non empty directories too. I bet everyone out there dealing with the file system had faced this problem.

If you do a wildcard or other search for a particular file and the file doesn't exist, Solaris FTP servers send back "search*.xml: No such file or directory" as a single element in the response array. This makes it hard to check for the presence of a file.

One possible work-around is to parse your own raw directory listing. Or you could check for the error verbatim. But different servers could return different values. A simpler way to compensate is to use the ftp_size command to validate the returned file:

There are some FTP servers that just dont accept the ftp_nlist command, they return a bool variable. but I think all of them accept the LIST command which is the ftp_rawlist command. So yu have to parse rawlist to nlist. I found a code here posted by "fredrik" but it shows a bug if the files at the ftp server have spaces oon theeir name... so here its a better function:function rawlist_2_nlist($list) { $newlist = array(); reset($list); while (list(,$row) = each($list)) { $buf=""; if ($row[0]=='d'||$row[0]=='-'){ $buf = substr($row,55); $newlist[]=$buf; } }

A note to developers using PHP on Windows servers: as of PHP 4.1.2, ftp_nlist() is broken in the Windows build of PHP. It will return nothing at all, even if the same code works fine on UNIX. So if you're going crazy trying to figure out why the function isn't returning anything, stop wasting your time, you're not doing anything wrong. Hopefully this will get fixed in future versions, although it's apparently been an issue since at least 4.0.6.