/ Published in: PHP
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
/** * Create a new directory, and the whole path. * * If the parent directory does not exists, we will create it, * etc. * @todo * - PHP5 mkdir functoin supports recursive, it should be used * @author baldurien at club-internet dot fr * @param string the directory to create * @param int the mode to apply on the directory * @return bool return true on success, false else * @previousNames mkdirs */ function makeAll($dir, $mode = 0777, $recursive = true) { return FALSE; } return TRUE; } } return FALSE; } /** * Copies file or folder from source to destination, it can also do * recursive copy by recursively creating the dest file or directory path if it wasn't exist * Use cases: * - Src:/home/test/file.txt ,Dst:/home/test/b ,Result:/home/test/b -> If source was file copy file.txt name with b as name to destination * - Src:/home/test/file.txt ,Dst:/home/test/b/ ,Result:/home/test/b/file.txt -> If source was file Creates b directory if does not exsits and copy file.txt into it * - Src:/home/test ,Dst:/home/ ,Result:/home/test/** -> If source was directory copy test directory and all of its content into dest * - Src:/home/test/ ,Dst:/home/ ,Result:/home/**-> if source was direcotry copy its content to dest * - Src:/home/test ,Dst:/home/test2 ,Result:/home/test2/** -> if source was directoy copy it and its content to dest with test2 as name * - Src:/home/test/ ,Dst:/home/test2 ,Result:->/home/test2/** if source was directoy copy it and its content to dest with test2 as name * @todo * - Should have rollback so it can undo the copy when it wasn't completely successful * - It should be possible to turn off auto path creation feature f * - Supporting callback function * - May prevent some issues on shared enviroments : <a href="http://us3.php.net/umask" title="http://us3.php.net/umask">http://us3.php.net/umask</a> * @param $source //file or folder * @param $dest ///file or folder * @param $options //folderPermission,filePermission * @return boolean */ { $result=false; //For Cross Platform Compatibility $options['noTheFirstRun']=true; } makeAll($dest,$options['folderPermission'],true); } } else { $__dest=$dest; } //Copy only contents } else { //Change parent itself and its contents } } else { //Copy parent directory with new name and all its content } else { //Copy parent directory with new name and all its content } } { if($file!="." && $file!="..") { $__dest=$dest."/".$file; $__source=$source."/".$file; //echo "$__source ||| $__dest<br />"; if ($__source!=$dest) { $result=smartCopy($__source, $__dest, $options); } } } } else { $result=false; } return $result; }
URL: http://sina.salek.ws/content/unix-smart-recursive-filefolder-copy-function-php