Got it! You want to write a function that takes three parameters – the source folder path, the target folder path, and the file extension – and moves files with that specific extension from the source folder to the target folder. Here's an example of the code:
function moveFilesByExtension($sourceDir, $targetDir, $fileExtension) {
if (!is_dir($sourceDir) || !is_dir($targetDir)) {
echo '源文件夹或目标文件夹不存在';
} else {
if ($dh = opendir($sourceDir)) {
while (($file = readdir($dh)) !== false) {
if (substr($file, -strlen($fileExtension)) === $fileExtension) {
rename("$sourceDir/$file", "$targetDir/$file");
}
}
closedir($dh);
}
}
}
// 调用函数
moveFilesByExtension('./回收站', './句子采集', '.txt');In this example, I define an object namedmoveFilesByExtensionThis function accepts three parameters: the source folder path, the destination folder path, and the file extension. Within the function, I employ logic similar to that used previously to perform the file relocation operation.
You can call this function according to your specific needs and pass in the appropriate parameters. We hope this meets your requirements; if you have any further questions, feel free to ask!