且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

将所有文件和文件夹从一个目录复制到另一个目录PHP

更新时间:2022-11-28 17:49:47

您好,几乎没有PHP开发人员,这不是一个问题,而是一个有关如何将文件从一个文件夹复制到另一个文件夹的问题的答案.我在互联网上遇到过一些开发人员使用rename()而不是copy()将文件从一个目录移动到另一个目录的情况. 这是简单的工作代码.经过测试,像魅力一样工作.

Hello there little php developers this not a question but an answer to a question on how to copy files from one folder to another. I have come across over the internet that some developers use rename()instead of copy() to move files from one directory to another. Here is simple working code. Tested and work like charm.

< ==========================魔术师================ ============

<===========================The Magic============================>

<?php    
    $dir = "path/to/targetFiles/";
    $dirNew="path/to/newFilesFolder/";
    // Open a known directory, and proceed to read its contents
    if (is_dir($dir)) {
        if ($dh = opendir($dir)) {
            while (($file = readdir($dh)) !== false) {
            //exclude unwanted 
            if ($file==".") continue;
            if ($file=="..")continue;
            //if ($file=="index.php") continue; for example if you have index.php in the folder

            if (copy("$dir/$file","$dirNew/$file"))
                {
                echo "Files Copyed Successfully";
                //echo "<img src=$dirNew/$file  />"; 
                //if files you are moving are images you can print it from 
                //new folder to be sure they are there 
                }
                else {echo "File Not Copy";}
            }
            closedir($dh);
        }
    }


    ?>