|Last Updated||Ratings||Unique User Downloads||Download Rankings|
|2016-10-29 (2 years ago)||Not enough user ratings||Total: 247||All time: 7,760 This week: 349|
|recreate-tree 1.1||GNU General Publi...||5.0||PHP 5, Files and Folders|
This class can recreate directory trees and copy their files.
Showing PHP for Obtaining Directory Trees
CLASS COPYING RECURSIVELY A DIRECTORY FROM A SOURCE PATH TO A DESTINATION PATH, WITH ALL OF SUBFOLDERS AND FILES HAVING THE PERMISSION ACCORDING TO SPECIFIC SETTINGS, WITH FOLDERS AND FILES OVERWRITTEN IF FOUND IN THE DESTINATION PATH, OR CREATED IF THEY DO NOT EXIST
Alessandro Quintiliani <alex23rps at gmail dot com>
GNU GPL (see file COPYING.txt)
PHP >= 5
1) Place Class.RecreateTree.php and Class.CustomExceptions.php in the root directory of your web application. If you still have a directory containing other classes in your web application, place Class.RecreateTree.php and Class.CustomExceptions.php in the same directory, modifying theirs relative path
2) Include first Class.CustomExceptions.php and then Class.RecreateTree.php at the top of the PHP script or before the point in the script where a copy of an entire directory content is required
3) Instantiate RecreateTree class with the following syntax:
$rt = new RecreateTree();
4) Set (optionally) the absolute or relative path to the main source directory to be copied (including or not the trailing slash or backslash); if omitted, the absolute path to the php file where this method is invoked is taken as a default
5) Set (optionally) the absolute or relative path to the main destination directory (including or not the trailing slash or backslash); if omitted, the absolute path to the php file where this method is invoked is taken as a default
6) Set (optionally) the permission to the main destination directory and all of its subdirectories in octal code (0xyz, where x,y,z: 0 -> 7); if omitted, the permission assigned is the same of the most internal directory in the source subpath common with the destination path; if the source and destination path have no common subpath, a cautios default permission 0755 is assigned as a default
7) Set (optionally) the permission to all files under the main destination directory (and all of its subdirectories) in octal code (0xyz, where x,y,z: 0 -> 7); if omitted, all of the files will be assigned 0644 as a default permission
NOTICE: both the setPermissionFile() and setPermissionFiles() methods accept the same input argument and are interchangeable. The reason of this double choice is to let flexibility to the user in the choice of the method name according to the idea of setting permission to one (...File) or more files (...Files). In the rest of this document you will find setPermissionFile(s) as referring to both the methods
NOTICE: the steps 4, 5 ,6 ,7 are interchangeable
8) Call the method makeTree() to create the destination tree of folder, files and subfolders
So, if under abs/or/rel/path/to/src/maindir/named/srcrootdir/ you have the following structure
|---subdir1/ | |---file1.ext1 | |---file2.ext1 | |---file3.ext2 | |---subdir2/ | |---file4.ext2 | |---subdir3/ | |---file5.ext3 |---file6.ext1
and you want to copy the structure under srcrootdir to another directory such as abs/or/rel/path/to/dest/maindir/named/destrootdir, after calling the makeTree() method you will have the following structure
|---subdir1/ | |---file1.ext1 | |---file2.ext1 | |---file3.ext2 | |---subdir2/ | |---file4.ext2 | |---subdir3/ | |---file5.ext3 |--file6.ext1
with the permission on each folder and file according to the octal values given as an input argument respectfully to the setPermissionDir(s) and setPermissionFile(s) methods.
As above described, if the input argument to setPermissionDir(s) is omitted and, as an example, the source and the destination path have abs/or/rel/path/to as a common subpath (or a further subpath), all the destination directories ( dest/maindir/named/destrootdir and subdir1, subdir2, subdir3 ) are created with the same permission of the most internal directory in the subpath common to both source and destination path (to/)
NOTICE: if the input argument to both setPathSrc() and setPathDest() is a relative path according to the Windows format (backslash "\" as a directory separator) and the PHP script calling this class should be migrate to Unix platform mantaining the same relative paths (or vice versa), you do not need to change the directory separator on each path (Unix requires slash "/" as directory separator) because setPathSrc() and setPathDest() automatically change "\" or "/" accordingly to the directory separator of the operating system where the PHP script is going to run.
makeTree(), if invoked, also prints on the browser several messages as a confirmation of successfully creation tree and useful for statistics. All of the possible outputs are:
4 independent warning messages due to an exception
Messages of successful creation tree or failures, plus other messages for statistics
Place Class.RecreateTree.php, Class.CustomExceptions.php, example.php at the same level in a webroot directory, i.e. CreateTree on a web server having <my.domain.com> or localhost as a domain.
Choose any directory having several subfolders and files you want to copy and make sure that the chosen directory and all of its subfolders and files have the right permission to allow the default user of the web server to read the structure. If so, in example.php replace the argument 'absolute/or/relative/path/to/src/maindir/named/srcrootdir/' passed as an input argument to the $rt->setPathSrc() method with an absolute (or relative) the path to your chosen main source directory.
Then choose a destination directory and make sure that you can set any permission according to that you apply by the octal code given as an input argument to the setPermissionDir(s)() and setPermissionFile(s)() methods. If so, in example.php replace the argument 'absolute/or/relative/path/to/dest/maindir/named/destrootdir/' passed as an input argument to the $rt->setPathDest() method with an absolute (or relative) the path to your chosen main destination directory.
Open a browser and type in the URL bar:
After a while (depending on the number of subfolders and files to copy), when example.php has run, you will see on the browser the result of the method makeTree() (see the description of this method).
As a test, you can change source and destination path, set different permissions to the destination folders (or omit it) and files, as well as modify and/or remove folders and/or files under the source or the destination directory: you will see that new files and/or folders are created or overwritten in case of different content.
|Class.CustomExceptions.php||Class||Customized classes handling some exceptions|
|Class.RecreateTree.php||Class||This class creates a folder tree from a string|
|COPYING.txt||Lic.||License of use of Class.RecreateTree.php|
|example.php||Example||example of usage on Class RecreateTree|
|README.md||Doc.||description of usage on this class|
|Version Control||Unique User Downloads||Download Rankings|
|User Comments (1)|