Files
We can handle files using the static JFile class. Before we explore how to use JFile, we
need to import the relevant library:
jimport('joomla.filesystem.file');
We'll start with four of the more basic JFile methods, each of which is used to handle
the name of a file. The first of these is called makeSafe(). This method takes a string
and removes any unsafe characters for use as a filename. This is especially useful
when we allow users to enter a filename of their choice:
$filename = JRequest::getVar('filename');
$cleanFilename = JFile::makeSafe($filename);
The resultant value of $cleanFilename will be the same as $filename, except that
any unsafe characters will have been removed.
The parameter that we pass to the makeSafe() method must not be a
path to a file. If we do pass a path, the directory separators will be stripped.
If we are dealing with a path to a file we can use the getName() method to determine
the filename. Once we have done this, we can use the makeSafe() method to ensure
the filename is safe to use:
$fileName = JFile::getName($pathToFile);
$cleanFilename = JFile::makeSafe($filename);
If we want to determine the extension of a file, we can use the getExt() method;
this method works with a filename and with a path to a file.
Pages:
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489