问题描述
我在 FTP 上有一个具有动态文件名的文件.架构类似于:
I have a file on an FTP with a dynamic filename. The schema looks something like:
ABCD_2_EFGH_YYMMDD_YYMMDD_randomnumber_.TXT
日期 (YYMMDD
) 反映前一天和当天,randomnumber
值是文件中记录的计数,并且每天都在变化.
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.
我正在尝试使用 PHP 检索文件,但在使用通配符名称时遇到问题.以下是代码示例:
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() 期望参数 3 是有效路径
ftp_get() expects parameter 3 to be a valid path
我也尝试将 glob()
用于 $server_file
并收到相同的错误.
I have also tried using glob()
for $server_file
and receive the same error.
有谁知道如何在 ftp_get()
中使用动态文件名?
Does anyone know how to use dynamic filenames with ftp_get()
?
推荐答案
您可以使用带有模式的 ftp_nlist
从 ftp 文件夹中检索文件名.然后在循环中使用 ftp_get
You can use ftp_nlist
with pattern to retreive file names from ftp folder. And then use ftp_get
in loop
$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
}
}
这篇关于使用 PHP ftp_get() 检索带有通配符文件名的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!