我有一个包含大量文件的文件夹。我的目标是在备份文件后根据创建或修改日期逐年备份文件,我也想删除这些文件。
实际上我的服务器包含大小为 20GB 的 pdf 文件,我想备份这些文件,但每年应该有文件夹。但我不知道如何实现这一点。
$dir_path = "/pdfs/";
$pdf_arr = scandir($dir_path);
foreach($pdf_arr as $file) {
if($file ! == '.' && $file ! == '..') {
print_r($file);
}
}
最佳答案
// year and month wise backup
public function getLastModifiedFiles() {
$source = public_path("pdfs");
$destination = public_path("destination");
$smallest_time=INF;
$oldest_file='';
if ($handle = opendir($source)) {
while (false !== ($file = readdir($handle))) {
if($file !== '.' && $file !== '..') {
$time = filemtime($source.'/'.$file);
$mm = date('m', $time);
$yr = date('Y', $time);
\File::isDirectory($destination.'/'.$yr."/$mm") or \File::makeDirectory($destination.'/'.$yr."/$mm", 0777, true, true);
$moveFile="$destination/$yr/$mm/$file";
//dd($source.'/'.$file);
if(!is_dir($source.'/'.$file)) {
if (copy($source.'/'.$file, $moveFile))
{
unlink($source.'/'.$file);
}
}
}
}
closedir($handle);
}
}
关于php - 如何从 php 中的文件夹中按日期收集单独文件夹中的文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58246642/