简体   繁体   English

如何获取服务器上文件名的列表/数组?

[英]How to get the list/array of file names on server?

How do I get the list/array of filenames (without extension) on server. 如何获取服务器上文件名的列表/数组(无扩展名)。 I want filenames only (curl or ftp also can). 我只想要文件名(curl或ftp也可以)。

I have tried ftp_nlist and curl_getinfo but the output is not as expected. 我已经尝试过ftp_nlistcurl_getinfo但是输出不符合预期。

// connect and login to FTP server
$ftp_server = "user.com";
$ftp_username = "user";
$ftp_userpass = "demo";

$ftp_conn = ftp_connect($ftp_server) or die("Could not connect to 
$ftp_server");
$login = ftp_login($ftp_conn, $ftp_username, $ftp_userpass);

// get file list of current directory
$file_list = ftp_nlist($ftp_conn, "/user/new/");
var_dump($file_list);

// close connection
ftp_close($ftp_conn);

Assuming i have 4 zip files which are 1.zip,2.zip,3.zip,4.zip on the server http://user.com/user/new . 假设我在服务器http://user.com/user/new上有4个zip文件,分别是1.zip,2.zip,3.zip,4.zip

Expected: 预期:

1
2
3
4

Actual outptut: 实际输出:

Array([0]=> string(1) "." [1]=> string(2) ".." [2]=> string(5) "1.zip" [3]=> string(5) "2.zip" [4]=> string(5) "3.zip" [5]=> string(5) "4.zip")

You can get the desired result using the following post-processing call: 您可以使用以下后期处理调用获得所需的结果:

$result = array_filter(array_map(function ($v) {
    return explode('.', $v)[0];
}, $file_names));

I think actual output is logically correct because in Linux system . 我认为实际输出在逻辑上是正确的,因为在Linux系统中 is the current directory, while .. signifies the parent directory. 是当前目录,而..表示父目录。 It makes things quicker at the command line as well so you don't need to type out full paths. 它也使命令行处理变得更快,因此您无需键入完整路径。 it is present every directory structure in operating systems so if you want your expect output then just filler or skip value of . 它存在于操作系统中的每个目录结构中,因此,如果您希望获得期望的输出,则只需填充或跳过的值。 and .. from return array. 和..从返回数组。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM