如何从ftp服务器下载最新的3个文件?
我已经尝试过using(array_slice($contents, -3, 3, true)
and,它在显示内容的var_dump
__for上运行得很好,这里只显示了3个最新的文件
我需要下载这3个文件,并将其保存在本地计算机* pv_inverter_2
pv_inverter_1
**和pv_inverter_1
和 pv_inverter_3
上。
我也可以使用array_slice(???, -3, 3, true)
这样做吗?
// get contents of the current directory
$contents = ftp_nlist($conn_id, ".");
// output $contents
var_dump (array_slice($contents, -3, 3, true));
$mostRecent = array(
'time' => 0,
'file' => null
);
foreach ($contents as $file) {
// get the last modified time for the file
$time = ftp_mdtm($conn_id, $file);
if ($time > $mostRecent['time']) {
// this file is the most recent so far
$mostRecent['time'] = $time;
$mostRecent['file'] = $file;
}
}
if (ftp_get($conn_id, "pv_inverter_1.csv", $mostRecent['file'],FTP_BINARY)) {
echo "Files Successfully Downloaded\n";
}
else {
echo "There was a problem\n";
}
发布于 2017-12-08 11:53:49
如果您想下载目录列表中服务器返回的最后三个文件:
$files = ftp_nlist($conn_id, ".");
$last_3_files = array_slice($files, -3);
$i = 0;
foreach ($last_3_files as $file)
{
if (ftp_get($conn_id, "pv_inverter_$i.csv", $file, FTP_BINARY))
{
echo "$file downloaded\n";
}
else
{
echo "There was a problem downloading $file\n";
}
$i++;
}
不过请注意,“最后”和“最新”并不相同。
https://stackoverflow.com/questions/47682338
复制相似问题