What PHP functions can be used to read files from a directory and copy them to another directory?
To read files from a directory and copy them to another directory in PHP, you can use the opendir(), readdir(), and copy() functions. First, open the source directory using opendir(), then loop through the files in the directory using readdir(), and copy each file to the destination directory using copy().
$sourceDir = '/path/to/source/directory/';
$destDir = '/path/to/destination/directory/';
$dirHandle = opendir($sourceDir);
while (($file = readdir($dirHandle)) !== false) {
if ($file != '.' && $file != '..') {
copy($sourceDir . $file, $destDir . $file);
}
}
closedir($dirHandle);
Keywords
Related Questions
- What are the best practices for displaying additional information in a "fly-over" window when hovering over a specific field in a table or <div> element?
- How can one effectively troubleshoot issues related to generating graphs using JPGraph in PHP?
- What are the advantages of using RecursiveDirectoryIterator over scandir in PHP?