<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://docs.sandbox.joomla.org/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Adjei7</id>
	<title>Joomla! Documentation - User contributions [en]</title>
	<link rel="self" type="application/atom+xml" href="https://docs.sandbox.joomla.org/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Adjei7"/>
	<link rel="alternate" type="text/html" href="https://docs.sandbox.joomla.org/Special:Contributions/Adjei7"/>
	<updated>2026-05-25T22:54:20Z</updated>
	<subtitle>User contributions</subtitle>
	<generator>MediaWiki 1.43.0</generator>
	<entry>
		<id>https://docs.sandbox.joomla.org/index.php?title=How_to_use_the_filesystem_package&amp;diff=644481</id>
		<title>How to use the filesystem package</title>
		<link rel="alternate" type="text/html" href="https://docs.sandbox.joomla.org/index.php?title=How_to_use_the_filesystem_package&amp;diff=644481"/>
		<updated>2019-12-09T08:10:32Z</updated>

		<summary type="html">&lt;p&gt;Adjei7: /* Example */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{version|1.5,2.5,3.x}}&lt;br /&gt;
=Using the JFile:: class=&lt;br /&gt;
&lt;br /&gt;
There are 4 classes in the filesystem library.&lt;br /&gt;
&lt;br /&gt;
* JFile:: (file.php)&lt;br /&gt;
* JFolder:: (folder.php)&lt;br /&gt;
* JPath:: (path.php)&lt;br /&gt;
* JArchive:: (archive.php)&lt;br /&gt;
&lt;br /&gt;
This tutorial focuses on the JFile:: and JFolder:: classes.&lt;br /&gt;
&lt;br /&gt;
The base for file handling is the JFile class, found in the &#039;&#039;/libraries/src/Filesystem/File.php&#039;&#039; file.&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
==Get the File Extension==&lt;br /&gt;
Syntax:&lt;br /&gt;
&amp;lt;source lang=php&amp;gt;&lt;br /&gt;
$ext =  JFile::getExt($filename);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Pretty clear, just feed the function with a filename and it will return the extension of the file you selected.&lt;br /&gt;
&lt;br /&gt;
==Strip the File Extension==&lt;br /&gt;
Syntax:&lt;br /&gt;
&amp;lt;source lang=php&amp;gt;&lt;br /&gt;
$name = JFile::stripExt($filename);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
This will return the filename without the extension.&lt;br /&gt;
&lt;br /&gt;
==Clean the Filename==&lt;br /&gt;
Use: &lt;br /&gt;
It cleans out all odd characters from a filename and returns a safe filename.&lt;br /&gt;
&lt;br /&gt;
Syntax:&lt;br /&gt;
&amp;lt;source lang=php&amp;gt;&lt;br /&gt;
$safefilename = File::makeSafe($filename);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
REQUIRES:&lt;br /&gt;
&amp;lt;source lang=php&amp;gt;&lt;br /&gt;
use Joomla\CMS\Filesystem\File;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&amp;lt;i&amp;gt;Normally at the head of the file.&amp;lt;i&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Copy a File==&lt;br /&gt;
Syntax:&lt;br /&gt;
&amp;lt;source lang=php&amp;gt;&lt;br /&gt;
JFile::copy($src, $dest);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
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. The great thing is that this function also makes use of the FTP-layer in J15 if necessary.&lt;br /&gt;
&lt;br /&gt;
==Delete a File==&lt;br /&gt;
Syntax:&lt;br /&gt;
&amp;lt;source lang=php&amp;gt;&lt;br /&gt;
JFile::delete($path.$file);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
It tries to delete the file, making sure it actually exists, 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.&lt;br /&gt;
&lt;br /&gt;
==Upload a File==&lt;br /&gt;
Syntax:&lt;br /&gt;
&amp;lt;source lang=php&amp;gt;&lt;br /&gt;
JFile::upload($src, $dest);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
It is basically the wrapper for the PHP move_uploaded_file() function, but also checks availability and permissions on both source and destination path.&lt;br /&gt;
&lt;br /&gt;
==Example==&lt;br /&gt;
So how does that look in a script? Here&#039;s a small code snippet of an upload script. Some of the functions are used.&lt;br /&gt;
The script is fired from an upload form. That form has a file element, called file_upload (see below).&lt;br /&gt;
&amp;lt;source lang=php&amp;gt;&lt;br /&gt;
&amp;lt;form name=&amp;quot;upload&amp;quot; method=&amp;quot;post&amp;quot; enctype=&amp;quot;multipart/form-data&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;input type=&amp;quot;file&amp;quot; name=&amp;quot;file_upload&amp;quot; /&amp;gt;&lt;br /&gt;
&amp;lt;input type=&amp;quot;submit&amp;quot; /&amp;gt;&lt;br /&gt;
&amp;lt;/form&amp;gt;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Note:&lt;br /&gt;
If you didn&#039;t add the part: &#039;&#039;enctype=&amp;quot;multipart/form-data&amp;quot;&#039;&#039; then you are unable to upload a file.&lt;br /&gt;
&lt;br /&gt;
The upload code looks like this:&lt;br /&gt;
&amp;lt;source lang=php&amp;gt;&lt;br /&gt;
&amp;lt;?php&lt;br /&gt;
/*&lt;br /&gt;
* File upload example&lt;br /&gt;
*/&lt;br /&gt;
// Retrieve file details from uploaded file, sent from upload form&lt;br /&gt;
$file = JFactory::getApplication()-&amp;gt;input-&amp;gt;files-&amp;gt;get(&#039;file_upload&#039;);&lt;br /&gt;
&lt;br /&gt;
// Import filesystem libraries. Perhaps not necessary, but does not hurt.&lt;br /&gt;
jimport(&#039;joomla.filesystem.file&#039;);&lt;br /&gt;
&lt;br /&gt;
// Clean up filename to get rid of strange characters like spaces etc.&lt;br /&gt;
$filename = JFile::makeSafe($file[&#039;name&#039;]);&lt;br /&gt;
&lt;br /&gt;
// Set up the source and destination of the file&lt;br /&gt;
$src = $file[&#039;tmp_name&#039;];&lt;br /&gt;
$dest = JPATH_COMPONENT . DS . &amp;quot;uploads&amp;quot; . DS . $filename;&lt;br /&gt;
&lt;br /&gt;
// First verify that the file has the right extension. We need jpg only.&lt;br /&gt;
if (strtolower(JFile::getExt($filename)) == &#039;jpg&#039;) &lt;br /&gt;
{&lt;br /&gt;
   // TODO: Add security checks.&lt;br /&gt;
&lt;br /&gt;
   if (JFile::upload($src, $dest))&lt;br /&gt;
   {&lt;br /&gt;
      // Redirect to a page of your choice.&lt;br /&gt;
   } &lt;br /&gt;
   else&lt;br /&gt;
   {&lt;br /&gt;
      // Redirect and throw an error message.&lt;br /&gt;
   }&lt;br /&gt;
}&lt;br /&gt;
else&lt;br /&gt;
{&lt;br /&gt;
   // Redirect and notify user file does not have right extension.&lt;br /&gt;
}&lt;br /&gt;
?&amp;gt;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Using the JFolder:: Class=&lt;br /&gt;
The base for folder handling is the JFolder class, found in the &#039;&#039;/libraries/src/Filesystem/Folder.php&#039;&#039; file.&lt;br /&gt;
Below I will discuss the most common options from this library file. At the end there is a simple example script.&lt;br /&gt;
&lt;br /&gt;
==Copy Folder==&lt;br /&gt;
Syntax:&lt;br /&gt;
&amp;lt;source lang=php&amp;gt;&lt;br /&gt;
JFolder::copy($src, $dest, $path, $force);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
This will copy a complete folder and all of its contents to another location on the server. It does permission and availability checking on both source and destination. By using &#039;&#039;$path&#039;&#039; you can enter a base path 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.&lt;br /&gt;
&lt;br /&gt;
==Create Folder==&lt;br /&gt;
Syntax:&lt;br /&gt;
&amp;lt;source lang=php&amp;gt;&lt;br /&gt;
JFolder::create($path, $mode);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
This is basically a wrapper for the PHP &#039;&#039;mkdir()&#039;&#039; function, but with error permissions and availability checking. This one also uses the FTP-layer when set in the configuration. &#039;&#039;$mode&#039;&#039; will set the default permission, once copied and defaults to 0755.&lt;br /&gt;
&lt;br /&gt;
If any &#039;&#039;parent&#039;&#039; directory does not exist, it is created. If the directory to be created ($path) already exists, this function just returns a Boolean true.&lt;br /&gt;
&lt;br /&gt;
==Move folder==&lt;br /&gt;
Syntax:&lt;br /&gt;
&amp;lt;source lang=php&amp;gt;&lt;br /&gt;
JFolder::move($src, $dest);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
Basically a wrapper for the PHP &#039;&#039;rename()&#039;&#039; function, but with permissions and availability checking. This one also uses the FTP-layer when set in the configuration.&lt;br /&gt;
&lt;br /&gt;
==Check If a Folder Exists==&lt;br /&gt;
Syntax:&lt;br /&gt;
&amp;lt;source lang=php&amp;gt;&lt;br /&gt;
JFolder::exists($path);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
Wrapper for the PHP &#039;&#039;is_dir()&#039;&#039; function. Returns true if the folder exists.&lt;br /&gt;
&lt;br /&gt;
==Read Files from a Folder==&lt;br /&gt;
Syntax:&lt;br /&gt;
&amp;lt;source lang=php&amp;gt;&lt;br /&gt;
JFolder::files($path, $filter = &#039;.&#039;, $recurse, $fullpath , $exclude);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
A function to read files from a folder. When setting &#039;&#039;$recurse&#039;&#039; to true, subfolders will also be searched. &#039;&#039;$fullpath&#039;&#039; set to true returns the full path in the array. With &#039;&#039;$exclude&#039;&#039;, you can offer an array of extensions, not to include in the search for files. It returns an array with all filenames.&lt;br /&gt;
&lt;br /&gt;
==Read Folders from Filesystem==&lt;br /&gt;
Syntax:&lt;br /&gt;
&amp;lt;source lang=php&amp;gt;&lt;br /&gt;
JFolder::folders($path, $filter = &#039;.&#039;, $recurse, $fullpath , $exclude);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
The same as &#039;&#039;JFolder::files()&#039;&#039;, except this only returns an array with folder names.&lt;br /&gt;
&lt;br /&gt;
==Make a Tree-Like List from a Folder Structure==&lt;br /&gt;
Syntax:&lt;br /&gt;
&amp;lt;source lang=php&amp;gt;&lt;br /&gt;
JFolder::listFolderTree($path, $filter, $maxLevel = 3, $level = 0, $parent = 0);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
It will read a folder, specified in &#039;&#039;$path&#039;&#039; 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:&lt;br /&gt;
&amp;lt;source lang=php&amp;gt;&lt;br /&gt;
Array&lt;br /&gt;
(&lt;br /&gt;
    [0] =&amp;gt; Array&lt;br /&gt;
        (&lt;br /&gt;
            [id] =&amp;gt; 1&lt;br /&gt;
            [parent] =&amp;gt; 0&lt;br /&gt;
            [name] =&amp;gt; administrator&lt;br /&gt;
            [fullname] =&amp;gt; g:/joomla_1012/administrator&lt;br /&gt;
            [relname] =&amp;gt; g:/joomla_1012/administrator&lt;br /&gt;
        )&lt;br /&gt;
&lt;br /&gt;
    [1] =&amp;gt; Array&lt;br /&gt;
        (&lt;br /&gt;
            [id] =&amp;gt; 2&lt;br /&gt;
            [parent] =&amp;gt; 1&lt;br /&gt;
            [name] =&amp;gt; backups&lt;br /&gt;
            [fullname] =&amp;gt; g:/joomla_1012/administrator/backups&lt;br /&gt;
            [relname] =&amp;gt; g:/joomla_1012/administrator/backups&lt;br /&gt;
        )&lt;br /&gt;
&lt;br /&gt;
    [2] =&amp;gt; Array&lt;br /&gt;
        (&lt;br /&gt;
            [id] =&amp;gt; 3&lt;br /&gt;
            [parent] =&amp;gt; 1&lt;br /&gt;
            [name] =&amp;gt; components&lt;br /&gt;
            [fullname] =&amp;gt; g:/joomla_1012/administrator/components&lt;br /&gt;
            [relname] =&amp;gt; g:/joomla_1012/administrator/components&lt;br /&gt;
        )&lt;br /&gt;
)&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Clean a Path String==&lt;br /&gt;
Syntax:&lt;br /&gt;
&amp;lt;source lang=php&amp;gt;&lt;br /&gt;
JFolder::makeSafe($path);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
Similar to the &#039;&#039;JFile::makeSafe()&#039;&#039; function. It cleans all odd characters out of the string and returns a clean path.&lt;br /&gt;
&lt;br /&gt;
==Example==&lt;br /&gt;
How does this look in actual code? We are going to read the contents of a folder called &#039;&#039;images&#039;&#039; that holds a number of files. We want to create a subfolder, called &#039;&#039;jpg&#039;&#039; and filter all jpg files out and move them to the &#039;&#039;jpg&#039;&#039; subfolder. After that, we will move the complete subfolder to another place on the server.&lt;br /&gt;
&amp;lt;source lang=php&amp;gt;&lt;br /&gt;
&amp;lt;?php&lt;br /&gt;
// First we set up parameters.&lt;br /&gt;
$searchpath = JPATH_COMPONENT . &#039;/images&#039;;&lt;br /&gt;
&lt;br /&gt;
// Import the folder system library.&lt;br /&gt;
jimport(&#039;joomla.filesystem.folder&#039;);&lt;br /&gt;
&lt;br /&gt;
// Then we create the subfolder called jpg.&lt;br /&gt;
if (!JFolder::create($searchpath . &amp;quot;/jpg&amp;quot;))&lt;br /&gt;
{&lt;br /&gt;
   // Throw error message and stop script.&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
// Now we read all jpg files and put them in an array.&lt;br /&gt;
$jpgFiles = JFolder::files($searchpath, &#039;.jpg&#039;);&lt;br /&gt;
&lt;br /&gt;
// Now we need some stuff from the &#039;&#039;JFile:: class&#039;&#039; to move all the files into the new folder.&lt;br /&gt;
foreach ($jpgFiles as $file)&lt;br /&gt;
{&lt;br /&gt;
   JFile::move($searchpath . &#039;/&#039; . $file, $searchpath . &#039;/&#039; . &#039;jpg&#039; . $file);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
// Last we move the complete subdir to the root of the component.&lt;br /&gt;
if (JFolder::move($searchpath . &#039;/&#039;. &#039;jpg&#039;, JPATH_COMPONENT))&lt;br /&gt;
{&lt;br /&gt;
   // Redirect with perhaps a happy message.&lt;br /&gt;
}&lt;br /&gt;
else&lt;br /&gt;
{&lt;br /&gt;
   // Throw an error.&lt;br /&gt;
}&lt;br /&gt;
?&amp;gt;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;noinclude&amp;gt;[[Category:Development]]&amp;lt;/noinclude&amp;gt;&lt;/div&gt;</summary>
		<author><name>Adjei7</name></author>
	</entry>
</feed>