How to use the filesystem package

From Joomla! Documentation

Using the JFILE:: class

There are 4 classes in the filesystem library.

  • JFile:: (file.php)
  • JFolder:: (folder.php)
  • JPath:: (path.php)
  • JArchive:: (archive.php)

This tutorial focuses on the JFile:: class.

The base for filehandling is the JFile class, found in libraries/joomla/filesystem/file.php Below you can see the most common options from this library file. At the end of this post you can find a very simple script for file upload.

Get file extension


$ext =  JFile::getExt($filename);

Pretty clear, just feed the function with a filename and it will return the extension of the file you selected.

Strip file extension


$name = JFile::stripExt($filename);

This will return the filename without the extension.

Clean filename


$safefilename = JFile::makeSafe($filename);

It cleans out all odd characters from a filename and returns a safe filename.

Copy a file


JFile::copy($src, $dest);

It is basically a wrapper for the PHP copy() function, but also checks if the file you want to copy exists and the destination really is available. Great thing is that this function also makes use of the FTP-layer in J15 if necessary.

Delete a file



It tries to delete the file, making sure it is actually existing, but also checks permissions. If permissions are not set up properly, it tries to change them and delete the file. It also uses the FTP-layer when necessary.

Upload a file


JFile::upload($src, $dest);

It is basically the wrapper for the PHP move_uploaded_file() function, but also does checks availability and permissions on both source and destination path.


So how does that look in a script? I put up a small code snippet of an upload script. Some of the functions are used. The script is fired from an upload form. That form has a file element, called file_upload (see below).

<form name="upload" method="post" enctype="multipart/form-data">
<input type="file" name="file_upload" />
<input type="submit" />

Note: If you didn´t add the part: enctype="multipart/form-data" then you are unable to upload a file!

Upload code looks like this:

* File upload example
// Retrieve file details from uploaded file, sent from upload form
$file = JFactory::getApplication()->input->get('file_upload');

// Import filesystem libraries. Perhaps not necessary, but does not hurt

// Clean up filename to get rid of strange characters like spaces etc
$filename = JFile::makeSafe($file['name']);

// Set up the source and destination of the file
$src = $file['tmp_name'];
$dest = JPATH_COMPONENT . DS . "uploads" . DS . $filename;

// First check if the file has the right extension, we need jpg only
if (strtolower(JFile::getExt($filename)) == 'jpg') 
   // TODO: Add security checks

   if (JFile::upload($src, $dest))
      // Redirect to a page of your choice
      // Redirect and throw an error message
   // Redirect and notify user file is not right extension

Using the JFolder:: class

The base for folder handling is the JFolder class, found in libraries/joomla/filesystem/folder.php Below 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 folder


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 folder


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 folder


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 exists



Wrapper for the php is_dir() function. Pretty straightforward, returns true if folder exists.

Read files from folder


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 filesystem


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 structure


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:

    [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 string



Identically to the JFile::makeSafe() function. It cleans all odd characters out of the string and returns a clean path.


Ok, 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.

// First we set up parameters
$searchpath = JPATH_COMPONENT . '/images';

// Import the folder system library

// Then we create the subfolder called jpg
if (!JFolder::create($searchpath . "/jpg"))
   // Throw error message and stop script

// Now we read all jpg files and put them in an array.
$jpgFiles = JFolder::files($searchpath, '.jpg');

// Now we need some stuff from the JFile:: class to move all files into the new folder
foreach ($jpgFiles as $file)
   JFile::move($searchpath . '/' . $file, $searchpath . '/' . 'jpg' . $file);

// Lastly, we are moving the complete subdir to the root of the component.
if (JFolder::move($searchpath . '/'. 'jpg', JPATH_COMPONENT))
   // Redirect with perhaps a happy message
   // Throw an error

This is example script only, but gives a general idea of the possibilities.