Last active
August 29, 2015 14:03
-
-
Save agungsijawir/4b875521870ae4f58c61 to your computer and use it in GitHub Desktop.
php copy recursive directory
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* $source = source path to be copied | |
* $dest = destination path | |
* | |
* Example $source directory structure | |
* c:/Sumber/ | |
* /file1.txt | |
* /file2.txt | |
* /file3.txt | |
* /folder1/ | |
* /folder1/file1-1.txt | |
* /folder1/file1-2.txt | |
* /folder1/file1-3.txt | |
* /folder2/ | |
* /folder2/file2-1.txt | |
* /folder2/file2-2.txt | |
* /folder2/file2-3.txt | |
* | |
* Source direcory has 2 sub-folders and each sub-folders has 3 files | |
* If destinaton directory doesn't exists, script will make new | |
*/ | |
$source = "c:/Lyrics"; | |
$dest= "c:/test2"; | |
// copy recursive | |
CopyRecursiveDir($source, $dest); | |
// creating zip from destination directory | |
echo "Creating zip... <br/>"; | |
Zip($dest, 'hasil.zip'); | |
// finish | |
echo "Finished!"; | |
echo "<a href='hasil.zip'>Download .zip</a>"; | |
function CopyRecursiveDir($source, $dest) | |
{ | |
if (!is_dir($dest)) { | |
echo "destination directory not exists, creating directory..."; | |
mkdir($dest . DIRECTORY_SEPARATOR, 0776, true); | |
} | |
foreach ($iterator = new RecursiveIteratorIterator( | |
new RecursiveDirectoryIterator($source, RecursiveDirectoryIterator::SKIP_DOTS), | |
RecursiveIteratorIterator::SELF_FIRST) as $item) | |
{ | |
if ($item->isDir()) { | |
mkdir($dest . DIRECTORY_SEPARATOR . $iterator->getSubPathName()); | |
echo "Creating " . $iterator->getSubPathName() . "...<br/>"; | |
} else { | |
copy($item, $dest . DIRECTORY_SEPARATOR . $iterator->getSubPathName()); | |
echo "Copying <strong>" . $item . "</strong> to " . $dest . "...<br/>"; | |
} | |
} | |
} | |
function Zip($source, $destination) | |
{ | |
if (!extension_loaded('zip') || !file_exists($source)) { | |
return false; | |
} | |
$zip = new ZipArchive(); | |
if (!$zip->open($destination, ZIPARCHIVE::CREATE)) { | |
return false; | |
} | |
$source = str_replace('\\', '/', realpath($source)); | |
if (is_dir($source) === true) | |
{ | |
$files = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($source), RecursiveIteratorIterator::SELF_FIRST); | |
foreach ($files as $file) | |
{ | |
$file = str_replace('\\', '/', $file); | |
// Ignore "." and ".." folders | |
if( in_array(substr($file, strrpos($file, '/')+1), array('.', '..')) ) | |
continue; | |
$file = realpath($file); | |
if (is_dir($file) === true) | |
{ | |
$zip->addEmptyDir(str_replace($source . '/', '', $file . '/')); | |
} else if (is_file($file) === true) { | |
$zip->addFromString(str_replace($source . '/', '', $file), file_get_contents($file)); | |
} | |
} | |
} else if (is_file($source) === true) { | |
$zip->addFromString(basename($source), file_get_contents($source)); | |
} | |
return $zip->close(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment