递归地复制一个特定的文件扩展名php


recursively copy a specific file extension php

我正在编写一个函数,以递归方式将特定的文件类型从一个文件夹复制到另一个文件夹,但该函数会复制文件夹中的所有文件。

function recurse_copy($src, $dst) {
    $dir = opendir($src);
    @mkdir($dst);
    while (false !== ( $file = readdir($dir))) {
        if (( $file != '.' ) && ( $file != '..' )) {
            if (is_dir($src . '/' . $file)) {
                if ($file->getExtension() == "pdf") {
                    recurse_copy($src . '/' . $file, $dst . '/' . $file);
                }
            } else {
                copy($src . '/' . $file, $dst . '/' . $file);
            }
        }
    } closedir($dir);
}
// if statements for 
$itp = new RecursiveDirectoryIterator("foldername/", > FilesystemIterator::SKIP_DOTS);
$displayp = Array('pdf');
$i = 0;
foreach (new RecursiveIteratorIterator($itp) as $filepop) {
    if (in_array(strtolower(array_pop(explode('.', $filepop))), $displayp))
        if ($filepop->getExtension() == "pdf") {
            echo >
            recurse_copy("a", "b");
        }
}
$itcopy = new RecursiveDirectoryIterator("foldername/", FilesystemIterator::SKIP_DOTS);
$displayp = Array ( 'pdf' );
foreach(new RecursiveIteratorIterator($itcopy) as $filecopy)
{
    if (in_array(strtolower(array_pop(explode('.', $filecopy))), $displayp))

    if ($filecopy->getExtension()=="pdf"){

        copy($filecopy->getrealPath(),'pdf_folder/'.$filecopy->getFilename()) ;

    }
 }