echo "Fertig";
$folder->close();
exit;
$folder->close();
exit;
/// check if given filesystem entry got modificated (in) between $min_hour and $max_hour
/// return bool() success
function within_mtime_hours(
$filepath, /// str(path:local)
$min_hour = 22, /// int(0...23)
$max_hour = 5 /// int(0...23)
) {
$mtime_hour = idate('H', filemtime($filepath));
return $mtime_hour >= $min_hour || $mtime_hour <= $max_hour;
}
/// copy source file to destination directory path
/// return bool() success
function copy_file(
$src_filepath, /// str(path:local) path to source file
$dst_dirpath = null, /// str(path:local) path to destination directory
$move = false /// bool() whether to move (copy + delete src file) or copy only
) {
if (!is_dir($dst_dirpath) || !is_file($src_filepath)) {
throw new InvalidArgumentException();
}
$dst_filepath = $dst_dirpath . DIRECTORY_SEPARATOR . basename($src_filepath);
if (true === $move) {
return rename($src_filepath, $dst_filepath);
}
if (!copy($src_filepath, $dst_filepath)) {
return false;
}
// restore mtime of source file
touch($dst_filepath, filemtime($src_filepath));
return true;
}
/// return int(0...) number of processed files | null() if directory scanning failed
function filter_and_process_files_in_dir(
$dirpath, /// str(path:local) path to directory
Callable $filter,
Callable $process
) {
$pwd = getcwd();
if (!chdir($dirpath)) {
return null;
}
if (!is_resource($dir = opendir($dirpath))) {
return null;
}
for ($processed = 0; is_string($entry = readdir($dir)); ) {
if (is_dir($entry)) {
continue;
}
// only files here
if ($filter(realpath($entry))) {
if ($process(realpath($entry))) {
++$processed;
}
}
}
closedir($dir);
chdir($pwd);
return $processed;
}
$src_dirpath = 'path_to_source_dir'; // Ausgangsverzeichnis; anpassen!
$my_copy_func = function ($src_filepath) {
return copy_file(
$src_filepath,
'path_to_destination_dir', // Zielverzeichnis; anpassen!
false // false: kopieren; true: verschieben
);
};
$num_files_processed = filter_and_process_files_in_dir(
$src_dirpath,
'within_mtime_hours',
$my_copy_func
);
var_dump($num_files_processed);
// INTEGER >= 0 wenn alles gut lief
// oder NULL, wenn Verzeichnis nicht eingelesen werden konnte
Kommentar