Along the same lines, we
can remove the extension from a filename; this also works with filenames and a path
to a file.
Utilities and Useful Classes
[ 352 ]
This example demonstrates how we can use these methods in conjunction with
one another:
if (JFile::getExt($filename) == 'txt')
{
echo JText::sprintf('%s is a text file',
JFile::stripExt($filename));
}
We will now venture into common file-system commands. We are provided with
four methods that deal explicitly with file management. The first of these is the
exists() method. This method returns a Boolean response and is used to check if a
file exists:
if (!JFile::exists($pathToFile))
{
// handle file does not exist
}
If a file exists then we can use any of the following methods to manage that file:
copy(), move(), and delete(). It's better to use these methods than to use the
normal PHP file-management functions because, if FTP is enabled, these methods
will attempt to use an FTP connection in priority to PHP functions. This decreases
the chance of error due to lack of user rights.
Pages:
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490