user10594468
user10594468

Reputation: 15

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

Upvotes: 0

Views: 102

Answers (2)

Abhijeet Jadhav
Abhijeet Jadhav

Reputation: 198

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.

Upvotes: 0

Steve Boyd
Steve Boyd

Reputation: 1357

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

Upvotes: 1

Related Questions