搜索文件名是否以String开头并通过PHP FTP返回结果

时间:2018-02-20 21:21:59

标签: php

示例:

awesome_dksd.php

awesome_dfkdde.php

dklf_dlsfd.php

示例路径:public_html / folder /

我如何使用php搜索那个ftp的一侧以获取以awesome_开头的文件我能够通过ftp_connect()进行连接我无法找到一个函数或任何方法在ftp中搜索一个文件以awesome_开始?

谢谢

2 个答案:

答案 0 :(得分:1)

使用ftp_nlist获取所有文件的列表 http://php.net/manual/en/function.ftp-nlist.php

// set up basic connection
$conn_id = ftp_connect($ftp_server);

// login with username and password
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);

// get contents of the current directory
$contents = ftp_nlist($conn_id, ".");

然后使用$contents循环遍历foreach或使用您最喜欢的正则表达式方法过滤掉任何不以 awesome _ 开头的内容!

答案 1 :(得分:0)

你可以尝试类似的东西(调整味道和php版本)

$ftpStream = ftp_connect (...);
// file listing
$files = ftp_nlist (  $ftpStream , string $directory );
// filters array with function that search position of string

$array = array_filter($array, function($value) {
    return !(strpos($value, 'awesome') > 0);
});

问候。