This method returns a
Boolean response. This example deletes the file foo.php from the frontend root of
the current component:
if (!JFile::delete(JPATH_COMPONENT.DS.'foo.php'))
{
// handle failed delete
}
If we want to delete multiple files at once, we can pass an array of file paths to the
delete() method. This example deletes the files foo.php and bar.php from the
frontend root of the current component:
$files = array(JPATH_COMPONENT.DS.'foo.php',
JPATH_COMPONENT.DS.'bar.php');
if (!JFile::delete($files))
{
// handle failed delete
}
The next two methods we will look at are used to read and write data to and from
files. These methods are aptly named read() and write(). We'll start by using the
read() method to access the contents of a file:
$file = JPATH_COMPONENT.DS.'foo.php';
$contents = JFile::read($file);
The contents of the file is read into the $contents variable as a string. If the read()
method is unsuccessful, the method returns false. It is not uncommon, once a file
is successfully read, to use the explode() function to split the contents into an array
of lines:
$lines = explode("\n", $contents);
To write to a file we use a similar approach.
Pages:
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492