通过FTP获取文件列表 [英] Getting the list of files over FTP

查看:1088
本文介绍了通过FTP获取文件列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



  

我想从FTP服务器打印文件列表和文件,这是我可以完成的。 <?php
$ ftp_server =我的ftp服务器;
$ conn_id = ftp_connect($ ftp_server);
$ ftp_user_name =ftp username;
$ ftp_user_pass =ftp密码;
$ login_result = ftp_login($ conn_id,$ ftp_user_name,$ ftp_user_pass);
$ contents = ftp_nlist($ conn_id,'/');
for($ i = 0; $ i echo< li> 。 substr($ contents [$ i],1)。 < /锂> 中;
ftp_close($ conn_id);
?>

但打印文件和文件夹的名称。如何才能打印文件的名称(文件可能没有扩展名!)

解决方案

选项:



1)您可以使用 ftp_rawlist 而不是 ftp_nlist 来获得完整的列表文件/目录,它应该表明它是否是一个目录。但是,该列表的格式将取决于ftp服务器的操作系统。例如,在unix / linux系统上,原始列表可能如下所示:

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

第一列中的d会告诉你它是一个目录。不知道它在Windows服务器上会是什么样子。



2)对于您返回的每个文件名,尝试将CD放入它。如果可以的话,这是一个目录!



$ p $ if(ftp_chdir($ conn_id,substr($ contents [$ i],1) ){
//这是一个目录,不要将它包含在你的列表中
ftp_cdup($ conn_id)//不要忘记回到你开始的目录!
}


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 the 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!
}

这篇关于通过FTP获取文件列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆