Vesthelm Engine

2.1.1 User Guide

File Class

The File class provides file functions.

Calling the File Class

$File = $this->File;

Getting File Extension

$ext = $this->File->getExtension("compile_dir/index.html");

echo $ext; //Outputs: html

Getting including dot:

$ext = $this->File->getExtension("compile_dir/index.html", true);

echo $ext; //Outputs: .html

Getting File Size

$file_size = $this->File->getSize("compile_dir/index.html");

echo $file_size; //Outputs (in bytes): 87

Formatting file size:

Size formats:

'Byte(s)', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'
$file_size = $this->File->getSize("compile_dir/index.html", 1);

echo $file_size; //Outputs: 87 Byte(s)

Returning formatted file size as array():

$file_size = $this->File->getSize("compile_dir/index.html", 0);

var_dump($file_size);

Outputs:

array(2) {
  ["size"]=>
  float(87)
  ["format"]=>
  string(7) "Byte(s)"
}

Getting File Contents

$this->File->setPath("compile_dir/"); //sets file folder !!!important!!!

$file_contents = $this->File->getContents('index.html');

echo $file_contents; //Outputs content from file

The second argument if true uses function unserialize() - creates a PHP value from a stored representation:

$php_code = $this->File->getContents('index.html', true);

Writing Contents to File

$this->File->setPath("compile_dir/"); //sets file folder !!!important!!!

$this->File->write('index.html', 'Test data');
or
$this->File->write('index.html', 'Test data', "compile_dir/");

The first argument is the filename. The second argument is the file contents. The third argument is the file folder.

Removing File

$this->File->setPath("compile_dir/"); //sets file folder !!!important!!!

$this->File->remove('index.html');
or
$this->File->remove('index.html', "compile_dir/");

Removing Directory Files

$this->File->setPath("compile_dir/"); //sets file folder !!!important!!!

$this->File->removeAll();
or
$this->File->removeAll(array(), "compile_dir/");

Excluding files from removing:

$removed_files = $this->File->removeAll(array('index.html', 'test.html'), "compile_dir/");

The first argument is the array of excluded files from removing. The second argument is the file folder.

Returns array of deleted filenames.

Creating Index File

$this->File->setPath("compile_dir/"); //sets file folder !!!important!!!

$this->File->createIndexFile();
or
$this->File->createIndexFile("compile_dir/", 'Test data');

The first argument is the file folder. The second argument is the file contents.