简体   繁体   中英

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).

I have tried ftp_nlist and curl_getinfo but the output is not as expected.

// 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 .

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 . 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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