[英]PHP: Open and show the most recent file from a folder via FTP
我想通过PHP连接到FTP服务器,并从某个目录中获取最新文件,并将其显示在该PHP文件中。
因此,我可以转到www.domain.com/file.php
并查看该文件中的内容。 这些文件具有以下名称“ Filename_20150721-085620_138.csv” ,因此第二个值20150721
是实际日期。 这些文件也只包含CSV文本。
有什么办法可以做到这一点?
欢迎来到Stackoverflow! 考虑以下代码和说明:
// connect
$conn = ftp_connect('ftp.addr.com');
ftp_login($conn, 'user', 'pass');
// get list of files on given path
$files = ftp_nlist($conn, '');
$newestfile = null;
$time = 0;
foreach ($files as $file) {
$tmp = explode("_", $file); // Filename_20150721-085620_138.csv => $tmp[1] has the date in question
$year = substr($tmp[1], 0, 4); // 2015
$month = substr($tmp[1], 4, 2); // 07
$day = substr($tmp[1], 6, 2); // 21
$current = strtotime("$month/$day/$year"); // makes a timestamp from a string
if ($current >= $time) { // that is newer
$time = $current;
$newestfile = $file;
}
}
ftp_close($conn);
之后,您的$newestfile
将保存最新的文件名。 这是你所追求的吗?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.