Actions

How to use the filesystem package

From Joomla! Documentation

Revision as of 09:33, 20 December 2008 by Ron273 (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Contents

Filehandling in Joomla 1.5

There are 4 classes in the J15 filesystem library.

  • JFile:: (file.php)
  • JFolder:: (folder.php)
  • JPath:: (path.php)
  • JArchive:: (archive.php)[/i]

For this first tutorial, I will focus on the JFile:: class.

The base for filehandling is the JFile class, found in libraries/joomla/filesystem/file.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 for file upload.

Get file extension

Syntax:

$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

Syntax:

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

This will return the filename without the extension.

Clean filename

Syntax:

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

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

Copy a file

Syntax:

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

Syntax:

JFile::delete($path.$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

Syntax:

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.

Example

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.

<?php
***********************
*                     *
* File upload example *
*                     *
***********************
//Retrieve file details from uploaded file, sent from upload form
$file = JRequest::getVar('file_upload', null, 'files', 'array');
 
//Import filesystem libraries. Perhaps not necessary, but does not hurt
jimport('joomla.filesystem.file');
 
//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') {
   if ( JFile::upload($src, $dest) ) {
      //Redirect to a page of your choice
   } else {
      //Redirect and throw an error message
   }
} else {
   //Redirect and notify user file is not right extension
}
?>