Skip to content

Instantly share code, notes, and snippets.

@Zenger
Created January 23, 2014 07:32
Show Gist options
  • Save Zenger/8574443 to your computer and use it in GitHub Desktop.
Save Zenger/8574443 to your computer and use it in GitHub Desktop.
Small class for moving whole directory structures. Unlike other solutions it builds up an array with directory structure. Possibly buggy :)
<?php
class LazyRecursiveMove
{
var $files = array();
var $dest = array();
var $from = "";
var $to = "";
function build_move_array( $from , $to, $parent = "" )
{
$files = array();
foreach( scandir($from) as $file )
{
if ($file != "." && $file != "..")
{
if (is_dir( $from . "/" . $file))
{
$ret = $this->build_move_array( $from . "/" . $file , $to, $file );
if (!empty($ret))
{
$files = $ret;
}
@mkdir($to . $parent . "/". $file );
}
else
{
$files[] = $from . "/". $file;
}
}
}
return $files;
}
function normalize_move_array( $array = false )
{
if (!$array)
{
$array = $this->files;
}
foreach($array as $i => $n)
{
$array[$i] = str_replace("//", "/", str_replace( $this->from, $this->to, $n) );
}
return $array;
}
function move( )
{
foreach($this->files as $i => $file )
{
copy( $this->files[$i], $this->dest[$i]);
echo "Copying " . $this->dest[$i] . "\n";
}
}
public function __construct($from , $to )
{
$this->from = $from;
$this->to = $to;
$this->files = $this->build_move_array( $from , $to );
$this->dest = $this->normalize_move_array();
$this->move();
}
}
new LazyRecursiveMove( getcwd() . '/upload/' , getcwd() . '/dest/' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment