使用PHP ftp_get()以通配符文件名检索文件 [英] Using PHP ftp_get() to retrieve file with wildcard filename
问题描述
我有一个带有动态文件名的FTP文件。模式如下所示:
ABCD_2_EFGH_YYMMDD_YYMMDD_randomnumber_.TXT
日期( YYMMDD
)反映前一天和当天,随机数
value是文件中记录的计数,并且每天都有所不同。
我试图使用PHP检索文件,但我无法使用通配符名称。这是一个代码示例:
<?php
$ yesterday = strtotime(' - 1 day' , 时间());
$ 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,'子目录名');
ftp_get($ conn_id,$ local_file,$ server_file,FTP_BINARY);
?>
运行此代码时,出现以下错误:
ftp_get()期望参数3是有效的路径
我也试过使用 glob()
用于 $ server_file
并且收到相同的错误。
有谁知道如何使用动态文件名与 ftp_get()
?
ftp_get
只能用于下载单个文件。不支持通配符。
唯一可靠的方法是列出所有文件,在本地过滤它们, :
$ conn_id = ftp_connect(ftp.example.com)或死(无法连接);
ftp_login($ conn_id,username,password)或死(无法登录);
ftp_pasv($ conn_id,true)或死(无法更改为被动模式);
$ files = ftp_nlist($ conn_id,/ path);
foreach($ files as $ file)
{
if(preg_match(/ \.txt $ / i,$ file))
{
回显Found $ file \\\
;
//下载ftp_get
}
}
大多数)服务器将允许您直接使用通配符:
$ conn_id = ftp_connect(ftp.example.com)或死(无法连接);
ftp_login($ conn_id,username,password)或死(无法登录);
ftp_pasv($ conn_id,true)或死(无法更改为被动模式);
$ files = ftp_nlist($ conn_id,/path/*.txt);
foreach($ files as $ file)
{
echoFound $ file\\\
;
//下载ftp_get
}
但这是一个非标准功能广泛支持)。
有关详细信息,请参阅我对 FTP目录部分与通配符列表。
I have a file on an FTP with a dynamic filename. 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.
I am attempting to retrieve the file using PHP but I am having trouble using a wildcard name. 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
I have also tried using glob()
for $server_file
and receive the same error.
Does anyone know how to use dynamic filenames with ftp_get()
?
The ftp_get
can be used to download a single file only. 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.
这篇关于使用PHP ftp_get()以通配符文件名检索文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!