Getting the list of files over FTP
I want to print the list of files and only files from an FTP server, here is what I could 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 this prints th开发者_如何学Goe names of files and folders. How can I just print the names of files (files may not have extensions!)
Options:
1) you can use ftp_rawlist
instead of ftp_nlist
to get the full listing for the file/directory, which should indicate whether it's a directory. However, the format of that listing will depend on the operating system of the ftp server. For example, on a unix/linux system the raw listing 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 it's a directory. Not sure what it would look like on a Windows server.
2) for each file name you return, try to CD into it. If you can, it's a directory!
if (ftp_chdir($conn_id, substr($contents[$i],1)) {
//it's 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!
}
Here is a script that will do it for you, courtesy of a poster on ftp_nlist (PHP Docs):
<?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";
}
}
?>
You could also use the ftp_mdtm()
function. This always returns an error (-1) when it fails which would indicate a directory since this function doesn't work for directories. The one caveat is that it doesn't work on some systems (the manual doesn't state which ones), so you'd need to test if it worked first.
if you use the ftp_rawlist like so:
$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]);
}
}
Should get you closer to your goal.
If you are in 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 remote server you can check if file exists with other methods.
精彩评论