简体   繁体   English

带有部分文件名的 ftp_get() 文件(不是通配符)

[英]ftp_get() file with partial filename(not a wildcard)

I need to get a file based on the second half of the filename with PHP我需要使用 PHP 根据文件名的后半部分获取文件

The structure of the filename will always be NAME_123456789.dat where the number is a tracking_id(unique) .文件名的结构将始终是 NAME_123456789.dat,其中数字是tracking_id(unique)

The name being John, Mel, Bronson, etc .名字是John, Mel, Bronson, etc And the number being a tracking_id .并且数字是tracking_id

What the process will be just for comprehension is that a person will enter their tracking_id.只是为了理解这个过程是一个人将输入他们的 tracking_id。 it will extract that from the search bar and plant it in the ftp search in the specific directory.它将从搜索栏中提取它并将其植入特定目录中的 ftp 搜索中。 Because the tracking_id is unique it should only return one result, hence ftp_get() right?因为tracking_id是唯一的,所以它应该只返回一个结果,因此ftp_get()对吗?

Any help is greatly appreciated.任何帮助是极大的赞赏。

Given the relatively small directory size (100+ from comments above), you should be ok first using ftp_nlist() to list all the files and then searching for and downloading the file you want.考虑到相对较小的目录大小(来自上面的评论 100+),您应该可以首先使用ftp_nlist()列出所有文件,然后搜索并下载您想要的文件。

$search = '_' . $trackingId . '.dat';
$searchLen = strlen($search);
$dir = '.'; // example directory
$files = ftp_nlist($connection, $dir); 
foreach ($files as $file) {
    // check if $file ends with $search
    if (strrpos($file, $search) === strlen($file) - $searchLen) {
        // found it, download it
        ftp_get($connection, 'some/local/file/path', $dir . '/' . $file);
    }
}

Better and more future-proof options can be found in Michael Berkowski's comment above ...可以在上面迈克尔·伯科夫斯基 (Michael Berkowski) 的评论中找到更好、更面向未来的选择......

How many files do you expect to be operating in the directory at any given time?在任何给定时间,您希望在目录中运行多少个文件? If it is a small number, listing the contents via ftp may work suitably.如果数量较少,通过 ftp 列出内容可能适用。 If it is many thousands of files, you might want to store some sort of text manifest file to read from, or index them in a database .如果有成千上万个文件,您可能希望存储某种文本清单文件以供读取或在数据库中索引它们。

These do hinge on how and when the files are uploaded to the FTP server though so given we don't know anything about that, I cannot provide any solutions.这些确实取决于文件上传到 FTP 服务器的方式和时间,但鉴于我们对此一无所知,我无法提供任何解决方案。

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

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