简体   繁体   English

使用 PHP ftp_get() 检索带有通配符文件名的文件

[英]Using PHP ftp_get() to retrieve file with wildcard filename

I have a file on an FTP with a dynamic filename.我在 FTP 上有一个动态文件名的文件。 The schema looks something like:架构看起来像:

ABCD_2_EFGH_YYMMDD_YYMMDD_randomnumber_.TXT

The dates ( YYMMDD ) reflect the previous day and current day and the randomnumber value is a count of the records in the file and varies each day.日期 ( YYMMDD ) 反映前一天和当天, randomnumber数值是文件中记录的计数,每天都在变化。

I am attempting to retrieve the file using PHP but I am having trouble using a wildcard name.我正在尝试使用 PHP 检索文件,但在使用通配符名称时遇到问题。 Here is a sample of the code:下面是代码示例:

<?php
$yesterday = strtotime('-1 day', time());
$today = strtotime('-0 day', time());
$local_file = "ABCD_2_EFGH__".date('Y-m-j', $yesterday).".txt";
$server_file = "ABCD_2_EFGH_".date('ymd', $yesterday)."_".date('ymd', $today)."_*.txt";

$conn_id = ftp_connect(ftpaddress);
$login_result = ftp_login($conn_id, 'username', 'password');
ftp_chdir($conn_id, 'subdirectory name');
ftp_get($conn_id, $local_file, $server_file, FTP_BINARY);
?>

When running this code i get the following error:运行此代码时,我收到以下错误:

ftp_get() expects parameter 3 to be a valid path ftp_get() 期望参数 3 是有效路径

I have also tried using glob() for $server_file and receive the same error.我也尝试将glob()用于$server_file并收到相同的错误。

Does anyone know how to use dynamic filenames with ftp_get() ?有谁知道如何在ftp_get()使用动态文件名?

You can use ftp_nlist with pattern to retreive file names from ftp folder.您可以使用带有模式的ftp_nlist从 ftp 文件夹中检索文件名。 And then use ftp_get in loop然后在循环中使用ftp_get

$fileList = ftp_nlist($conn_id, 'subdirectory_name/file_prefix_*.txt');

for ($i = 0; $i < count($fileList); $i++) {
    $localFile = tempnam(sys_get_temp_dir(), 'ftp_in');
    if (ftp_get($conn_id, $localFile, $fileList[$i], FTP_BINARY)){
      //do something
    }
}

The ftp_get can be used to download a single file only. ftp_get只能用于下载单个文件。 No wildcards are supported.不支持通配符。


The only reliable way is to list all files, filter them locally and then download them one-by-one:唯一可靠的方法是列出所有文件,在本地过滤它们,然后一一下载:

$conn_id = ftp_connect("ftp.example.com") or die("Cannot connect");
ftp_login($conn_id, "username", "password") or die("Cannot login");
ftp_pasv($conn_id, true) or die("Cannot change to passive mode");

$files = ftp_nlist($conn_id, "/path");

foreach ($files as $file)
{
    if (preg_match("/\.txt$/i", $file))
    {
        echo "Found $file\n";
        // download with ftp_get
    }
}

Some (most) servers will allow you to use a wildcard directly:一些(大多数)服务器将允许您直接使用通配符:

$conn_id = ftp_connect("ftp.example.com") or die("Cannot connect");
ftp_login($conn_id, "username", "password") or die("Cannot login");
ftp_pasv($conn_id, true) or die("Cannot change to passive mode");

$files = ftp_nlist($conn_id, "/path/*.txt");

foreach ($files as $file)
{
    echo "Found $file\n";
    // download with ftp_get
}

But that's a nonstandard feature (while widely supported).但这是一个非标准功能(虽然得到广泛支持)。

For details see my answer to FTP directory partial listing with wildcards .有关详细信息,请参阅我对使用通配符的 FTP 目录部分列表的回答。

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

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