What potential issues can arise when processing a large number of files in PHP and how can they be mitigated?

One potential issue when processing a large number of files in PHP is running out of memory due to loading all files into memory at once. To mitigate this, you can process files one at a time instead of loading them all at once.

// Open directory
$dir = '/path/to/directory';
$files = scandir($dir);

// Process files one at a time
foreach ($files as $file) {
    if ($file != '.' && $file != '..') {
        // Process file here
    }
}