Başka bir dizin PHP bir klasördeki tüm dosyaları ve klasörleri kopyalayın

5 Cevap php

Ben "mysourcedir" denilen dizin bu sonme dosya ve klasörleri var. bu yüzden PHP kullanarak Linux sunucu üzerinde diğer bazı "destinationfolder" Bu dizinde tüm içeriği kopyalamak istediğiniz.

function full_copy( $source, $target ) {
if ( is_dir( $source ) ) {
    @mkdir( $target );
    $d = dir( $source );
    while ( FALSE !== ( $entry = $d->read() ) ) {
        if ( $entry == '.' || $entry == '..' ) {
            continue;
        }
        $Entry = $source . '/' . $entry; 
        if ( is_dir( $Entry ) ) {
            $this->full_copy( $Entry, $target . '/' . $entry );
            continue;
        }
        copy( $Entry, $target . '/' . $entry );
    }

    $d->close();
}else {
    copy( $source, $target );
}
}

Ben bu kodu çalışıyorum, ama o, bu hedef konumda dizin "mysourcedir" biraz problem yaratır gelmez. Ben, sadece hedefe tüm dosyaları ve klasörleri kopyalamak için bekliyorum. Tavsiye edin

5 Cevap

class FolderCopy {


  public static function copyFolder($src, $dest) {

    $path = realpath($src);
    $objects = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path), RecursiveIteratorIterator::SELF_FIRST);

      /** SplFileInfo $object*/
    foreach($objects as $name => $object)
    {
      $startsAt = substr(dirname($name), strlen($src));
      self::mkDir($dest.$startsAt);
      if(is_writable($dest.$startsAt) and $object->isFile())
      {
          copy((string)$name, $dest.$startsAt.DIRECTORY_SEPARATOR.basename($name));
      }
    }
  }

  private static function mkDir($folder, $perm=0777) {
    if(!is_dir($folder)) {
      mkdir($folder, $perm);
    }
  }

}

FolderCopy :: CopyFolder (dirname (dirname (FILE)) "/ images", dirname (FILE) "/ test"..);

Bu benim öneri.

Muhtemelen sadece bu gibi, aşağı bu hat taşımak istiyorum:

function full_copy( $source, $target ) {
if ( is_dir( $source ) ) {
        $d = dir( $source );
        while ( FALSE !== ( $entry = $d->read() ) ) {
                if ( $entry == '.' || $entry == '..' ) {
                        continue;
                }
                $Entry = $source . '/' . $entry; 
                if ( is_dir( $Entry ) ) {
                        @mkdir( $Entry );
                        $this->full_copy( $Entry, $target . '/' . $entry );
                        continue;
                }
                copy( $Entry, $target . '/' . $entry );
        }

        $d->close();
}else {
        copy( $source, $target );
}

Şahsen ben onları ayırt etmek için sadece captilisation ile aynı adı taşıyan 2 değişkenleri kullanarak önleyeceğini rağmen. Diğer olasılıklar için http://www.php.net/copy üzerindeki kullanıcı yorumları kontrol edin.

Seni hedef dizinde yeniden yaratıyor neden $ d-> read () ayrıca ebeveyn ve şapka adını dönecektir olduğunu taht düşünüyorum.

CP-A çalıştırmayı deneyin. Bu mod süreleri ve izinleri koruyarak ilgilenir, ve tüm bu. cp -al bir hardlink çiftlik yapacaktır.

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.

<=========================== 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);
        }
    }


    ?>