Skip to content
PHP Logo

How can PHP be used to read and save all files from a folder?

To read and save all files from a folder in PHP, you can use the opendir() function to open the directory, loop through each file using readdir(), and then save the contents of each file into an array or perform any desired operation.

php
$directory = "/path/to/folder";
$files = [];

if ($handle = opendir($directory)) {
    while (false !== ($file = readdir($handle))) {
        if ($file != "." && $file != "..") {
            $files[] = file_get_contents($directory . "/" . $file);
        }
    }
    closedir($handle);
}

// Now $files array contains the contents of all files in the folder