Folder handling in Joomla 1.5There are 4 classes in the J15 filesystem library.
- JFile:: (file.php)
- JFolder:: (folder.php)
- JPath:: (path.php)
- JArchive:: (archive.php)
For this second tutorial, I will focus on the JFolder:: class.
The base for folder handling is the JFolder class, found in
libraries/joomla/filesystem/folder.phpBelow I will discuss the most common options from this library file. At the end of this post, I put it together in a very simple script as an exmaple
Copy folderSyntax:Code:
JFolder::copy($src, $dest, $path, $force);
This will copy a complete folder and all of it’s contents to another location on the server. It does permission and availability checking on both source and destination. By using $path you can enter a basepath to prefix to the filename and by setting the $force parameter to true, you can force the overwriting of already existing files. If set in the configuration, the FTP-layer will be used.
Create folderSyntax:Code:
JFolder::create($path, $mode);
This is basically a wrapper for the php
mkdir() function, but with error permissions and availability checking. This one also uses the FTP-layer when set in the configuration. $mode will set the default permission, once copied and defaults to 0755.
Move folderSyntax:Code:
JFolder::move($src, $dest);
Basically a wrapper for the php
rename() function, but with permissions and availability checking. This one also uses the FTP-layer when set in the configuration.
Check if folder existsSyntax:Code:
JFolder::exists($path);
Wrapper for the php
is_dir() function. Pretty straightforward, returns true if folder exists.
Read files from folderSyntax:Code:
JFolder::files($path, $filter = '.', $recurse, $fullpath , $exclude);
Function to read files from a folder. When setting $recurse to true, also subfolders will be searched. $fullpath set to true returns the full path in the array. With $exclude, you can offer an array of extensions, not to include in the search for files. It returns an array with all filenames.
Read folders from filesystemSyntax:Code:
JFolder::folders($path, $filter = '.', $recurse, $fullpath , $exclude);
Exactly the same as JFolder::files(), except this does only return an array with foldernames.
Make a tree like list from a folder structureSyntax:Code:
JFolder::listFolderTree($path, $filter, $maxLevel = 3, $level = 0, $parent = 0);
It will read a folder, specified in $path and will return all folders in an array, suitable for tree display. You can specify the number of levels. The folder array looks like this:
Quote:
Array
(
[0] => Array
(
[id] => 1
[parent] => 0
[name] => administrator
[fullname] => g:/joomla_1012/administrator
[relname] => g:/joomla_1012/administrator
)
[1] => Array
(
[id] => 2
[parent] => 1
[name] => backups
[fullname] => g:/joomla_1012/administrator/backups
[relname] => g:/joomla_1012/administrator/backups
)
[2] => Array
(
[id] => 3
[parent] => 1
[name] => components
[fullname] => g:/joomla_1012/administrator/components
[relname] => g:/joomla_1012/administrator/components
)
)
Clean a path stringSyntax:Code:
JFolder::makeSafe($path);
Identically to the JFile::makeSafe() function. It cleans all odd characters out of the string and returns a clean path.
ExampleOk, and how does this look in actual code. We are going to read the contents of a folder called images. In it, al large number of files are placed. We want to create a subfolder, called jpg and filter all jpg files out and move them to the jpg subfolder. After that, we will move the complete subfolder to another place on the server.
Code:
<?php
//First we set up parameters
$searchpath = JPATH_COMPONENT . DS . "images";
//Then we create the subfolder called jpg
if ( !JFolder::create($searchpath . DS . "jpg") ) {
//Throw error message and stop script
}
//Now we read all jpg files and put them in an array.
$jpg_files = JFolder::files($searchpath, '.jpg');
//Now we need some stuff from the JFile:: class to move all files into the new folder
foreach ($jpg_files as $file) {
JFile::move($searchpath. DS . $file, $searchpath . DS. "jpg" . $file);
}
//Lastly, we are moving the complete subdir to the root of the component.
if (JFolder::move($searchpath . DS. "jpg", $JPATH_COMPONENT) ) {
//Redirect with perhaps a happy message
} else {
//Throw an error
}
?>
This is example script only, but gives a general idea of the possibilities. I did not touch other stuff like JError to keep it clean and simple.
Good luck using JFolder.
Cheers,
Ronald