폴더를 통해 반복하고 그 안의 모든 파일을 삭제하는 스크립트를 찾고 있습니다.하지만 가장 최근의 파일 (각 파일의 이름을 filename_date('Y')_date('m')_date('d').extension)
으로 표시했습니다. 관련이 있는지 확실하지 않습니다).폴더 안의 모든 파일을 삭제 하시겠습니까?
나는 스택에 여기에이 스크립트를 발견 :
이if ($handle = opendir('/path/to/your/folder'))
{
$files = array();
while (false !== ($file = readdir($handle)))
{
if (!is_dir($file))
{
// You'll want to check the return value here rather than just blindly adding to the array
$files[$file] = filemtime($file);
}
}
// Now sort by timestamp (just an integer) from oldest to newest
asort($files, SORT_NUMERIC);
// Loop over all but the 5 newest files and delete them
// Only need the array keys (filenames) since we don't care about timestamps now as the array will be in order
$files = array_keys($files);
for ($i = 0; $i < (count($files) - 5); $i++)
{
// You'll probably want to check the return value of this too
unlink($files[$i]);
}
}
이 위의 마지막 다섯 아무것도하지만 삭제합니다. 이것이 좋은 방법일까요? 아니면 다른 방법, 더 간단하거나 더 좋은 방법이 있습니까?