如何使用PHP下载FTP上最新的文件? [英] How can I download the most recent file on FTP with PHP?
问题描述
我使用folowing脚本下载一个文件。
$ conn = ftp_connect(ftp.testftp.com)或死(无法连接);
ftp_login($ conn,user,pass);
ftp_get($ conn,target.txt,source.txt,FTP_ASCII);
ftp_close($ conn);
预先感谢!!!
没有办法确定哪个文件是最近的,因为没有上传时间属性这样的事情。你没有提及FTP服务器,但是如果你对上传有一定程度的管理,你可以确保最后修改时间是在上传时设置的。这个结果是否正常工作取决于您的FTP服务器以及可能的客户端。
假设您的修改时间与上传时间相同,您可以这样做:
//连接
$ conn = ftp_connect('ftp.addr.com');
ftp_login($ conn,'user','pass');
//获取给定路径上的文件列表
$ files = ftp_nlist($ conn,'');
$ mostRecent = array(
'time'=> 0,
'file'=> null
);
foreach($ files as $ file){
//获取文件的最后修改时间
$ time = ftp_mdtm($ conn,$ file);
if($ time> $ mostRecent ['time']){
//这个文件是目前最新的
$ mostRecent ['time'] = $时间;
$ mostRecent ['file'] = $ file;
}
}
ftp_get($ conn,target.txt,$ mostRecent ['file'],FTP_ASCII);
ftp_close($ conn);
On FTP server has some files. For any hour on this server is uploading new files. I'd like to download last file. How can I get last uploaded file from this server? All files are with different names.
I used folowing script to downloading one file.
$conn = ftp_connect("ftp.testftp.com") or die("Could not connect");
ftp_login($conn,"user","pass");
ftp_get($conn,"target.txt","source.txt",FTP_ASCII);
ftp_close($conn);
Thanks in advance !!!
There is no way to be sure which file is the most recent, as there is no such thing as an "uploaded time" attribute. You didn't mention much about the FTP server, but if you have some level of management over the uploads, you could ensure that the last modified time is set on upload. Whether this ends up working is down to your FTP server and possibly clients.
Assuming your modified times are equivalent to upload times, you could do something like this:
// connect
$conn = ftp_connect('ftp.addr.com');
ftp_login($conn, 'user', 'pass');
// get list of files on given path
$files = ftp_nlist($conn, '');
$mostRecent = array(
'time' => 0,
'file' => null
);
foreach ($files as $file) {
// get the last modified time for the file
$time = ftp_mdtm($conn, $file);
if ($time > $mostRecent['time']) {
// this file is the most recent so far
$mostRecent['time'] = $time;
$mostRecent['file'] = $file;
}
}
ftp_get($conn, "target.txt", $mostRecent['file'], FTP_ASCII);
ftp_close($conn);
这篇关于如何使用PHP下载FTP上最新的文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!