What are some best practices for optimizing performance when working with file and folder structures in PHP?

When working with file and folder structures in PHP, it is important to optimize performance by minimizing the number of file system operations and using efficient methods to access and manipulate files and folders. One best practice is to cache file and folder information to reduce the number of system calls. Another is to use relative paths instead of absolute paths to improve portability and avoid hardcoding directory structures. Additionally, using functions like opendir(), readdir(), and closedir() for directory traversal can be more efficient than recursive functions or glob().

// Example of caching file and folder information
$cache = [];

function getFileContents($filename) {
    global $cache;
    
    if(isset($cache[$filename])) {
        return $cache[$filename];
    } else {
        $content = file_get_contents($filename);
        $cache[$filename] = $content;
        return $content;
    }
}