What is the best way to automatically copy log files from multiple subdirectories into a single directory using PHP?
To automatically copy log files from multiple subdirectories into a single directory using PHP, you can use the RecursiveDirectoryIterator and RecursiveIteratorIterator classes to iterate through all subdirectories and files. You can then check if the file is a log file using pathinfo() function and copy it to the destination directory using copy() function.
$sourceDir = '/path/to/source/directory';
$destinationDir = '/path/to/destination/directory';
$iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($sourceDir));
foreach ($iterator as $file) {
if ($file->isFile() && pathinfo($file, PATHINFO_EXTENSION) == 'log') {
$destinationFile = $destinationDir . '/' . $file->getFilename();
copy($file, $destinationFile);
}
}
Related Questions
- What are the best practices for handling error messages and redirects in PHP scripts, especially when using header() function?
- How can PHP developers streamline the process of generating select options in forms to avoid repetitive code?
- What are the advantages of using an array in the name attribute of form fields in PHP?