简体   繁体   中英

recursively copy a specific file extension php

I am writing a function to recursively copy a specific file type from one folder to the other, but the function copies all the files in the folder.

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()) ;


    }

 }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM