[英]How do i download the 3 latest files from a ftp server using an array in php
我试过使用(array_slice($contents, -3, 3, true)
并且在var_dump
可以很好地显示内容,这里仅显示了3个最新文件
我需要使用这些名称下载3个文件并将其保存在本地计算机pv_inverter_1
和pv_inverter_2
和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";
}
如果要下载服务器在目录列表中返回的最后三个文件:
$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++;
}
但是请注意, “最后”和“最新”是不同的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.