Home > Code Snippets, PHP > Moving contents from one folder to another

Moving contents from one folder to another

October 25th, 2006 Leave a comment Go to comments

We always try to store images in a web path where heavy image linking is needed, and mostly the images will be categorised in some sort of directory tree.. to limit the no of files in a folder.

Once editing, for example, re assinging a set of students from one scientist to another, was needed, and the hell, we had to face.. finally the following piece of code was written, for a future reuse.

<?php
function mkdir_r($dirName$rights=0777){
        
$dirs explode('/'$dirName);
        
$dir='';
        foreach (
$dirs as $part) {
        
$dir.=$part.'/';
        if (!
is_dir($dir) && strlen($dir)>0)
                
mkdir($dir$rights);
        }
}
 
function 
xMove($source$target$drop true){
        if(!
is_dir($source)) return false;
        if(!
is_dir($target)) mkdir_r($target);
 
        
$d dir($source);
        while (
false !== ($entry $d->read())) {
                if(!
is_dir($source"/$entry")){
                        
copy($source "/$entry"$target "/$entry");
                        
unlink($source "/$entry");
                }
        }
        
$d->close();
        if(
$drop)
                
rmdir($source);
}
 
?>

If any body is wondering (just a thought) how I have managed the colour highlighting, then better take a look at Phpizer Plugin

  1. No comments yet.
  1. No trackbacks yet.

− one = one