Mecha CMS

Deep learning on using the API.

Class File

Author: Taufik Nurrohman · 86 Views

Tweet

Working with files and folders.

Configuration 

File::$config = [
    // `$min` is for the minimum file size allowed in byte(s)
    // `$max` is for the maximum file size allowed in byte(s)
    'sizes' => [$min, $max],
    // list of allowed file extension(s)
    'extensions' = ['txt']
];

Exist 

Check the existence of a file/folder.

File::exist($input, $fail = false);

Example:

if (File::exist('test.txt')) {
    echo 'File `test.txt` exists.';
}

Return the given path if file/folder exists, return false if not:

if ($file = File::exist('header.php')) {
    include $file;
} else {
    include 'header.legacy.php';
}

Shortcut:

include File::exist('header.php', 'header.legacy.php');

Loop through list of file path until it finds a file that exists:

if ($test = File::exist([
    'page-post-2016.php',
    'page-post.php',
    'page.php'
], false)) {
    echo $test . ' does exist.';
} else {
    echo $test . ' is now `false`.';
}

Inspect 

File::inspect($input, $key = null, $fail = false);

Inspect file details:

$test_1 = File::inspect('.\path\to\file.txt');
$test_2 = File::inspect('.\path\to\file.txt', 'url');
$test_3 = File::inspect('.\path\to\file.txt', 'wow');

Result:

$test_1 = [
    'path' => '.\path\to\file.txt',
    'name' => 'file',
    'url' => 'http://mecha-cms.com/path/to/file.txt',
    'extension' => 'txt',
    'update' => '2014-06-21 20:05:17',
    'size' => '20.62 KB',
    'is' => [
        'hidden' => false,
        'file' => true,
        'files' => false,
        'folder' => false // alias for `files`
    ],
    '_update' => 1403355917,
    '_size' => 21114
];

$test_2 = 'http://mecha-cms.com/path/to/file.txt';

$test_3 = false;

Explore 

File::explore($folder = ROOT, $deep = false, $fail = []);

Options:

ParameterDescription
$folderThe folder path to explore.
$deepExplore the folder contents recursively.

Example:

List all files and folders from a folder:

$tree_1 = File::explore('path\to\folder');
$tree_2 = File::explore('path\to\folder', true);

Result:

$tree_1
 │
 ├── path\to\folder\file-1.txt: 1
 ├── path\to\folder\file-2.txt: 1
 ├── path\to\folder\folder-1: 0
 ├── path\to\folder\folder-2: 0
 └── path\to\folder\folder-3: 0

$tree_2
 │
 ├── path\to\folder\file-1.txt: 1
 ├── path\to\folder\file-2.txt: 1
 ├── path\to\folder\folder-1: 0
 ├── path\to\folder\folder-1\file-1.txt: 1
 ├── path\to\folder\folder-1\file-2.txt: 1
 ├── path\to\folder\folder-1\file-3.txt: 1
 ├── path\to\folder\folder-2: 0
 ├── path\to\folder\folder-2\file-1.txt: 1
 ├── path\to\folder\folder-3: 0
 ├── path\to\folder\folder-3\file-1.txt: 1
 ├── path\to\folder\folder-3\file-3.txt: 1
 ├── path\to\folder\folder-3\folder-1: 0
 ├── path\to\folder\folder-3\folder-1\file-1.txt: 1
 ├── path\to\folder\folder-3\folder-1\file-2.txt: 1
 └── path\to\folder\folder-3\folder-1\file-3.txt: 1

$folder options:

$test_1 = File::explore(['path\to\folder', 1], true); // get file(s) only
$test_2 = File::explore(['path\to\folder', 0], true); // get folder(s) only
$test_3 = File::explore(['path\to\folder', 'css'], true); // get file(s) only with extension `css`
$test_4 = File::explore(['path\to\folder', 'css,js'], true); // get file(s) only with extension `css` and `js`

Write 

Create a file.

File::write('some text')->saveTo('path\to\file.txt');

Save 

Update a file.

File::open('path\to\file.txt')->write('some text')->save();

Append 

Append text to a file.

File::open('path\to\file.txt')->append('hey!')->save();

Prepend 

Prepend text to a file.

File::open('path\to\file.txt')->prepend('hey!')->save();

Read 

Output the file content.

echo File::open('path\to\file.txt')->read();

Get 

Reading file line by line. An alternative to the previous method which allows you to stop the reading process on a specific line/condition.

Example:

// stop reading at line 4
echo File::open('path\to\file.txt')->get(3);
// stop reading at a line that contains `$`
echo File::open('path\to\file.txt')->get('$');
// stop reading at a line that contains `$` exactly at position `12`
echo File::open('path\to\file.txt')->get(['$', 12]);

Import 

Import a PHP file.

$test = File::open('path\to\file.php')->import();

Export 

Export data as a PHP file.

$test = File::export([
    'foo' => 'bar',
    'baz' => 'qux'
])->saveTo('path\to\file.php');

Serialize 

This is just a shortcut for File::write(serialize('test!'))->saveTo('path\to\file.txt').

File::serialize('test!')->saveTo('path\to\file.txt');

Unserialize 

This is just a shortcut for unserialize(File::open('file.txt')->read()).

File::open('file.txt')->unserialize();

Move To 

Move a file or folder.

File::open('path\to\file.txt')->moveTo('path\to\folder');
File::open('path\to\folder')->moveTo('path\to\another-folder');

Move a file or folder, then rename it:

File::open('path\to\file.txt')->moveTo('path\to\folder\file-1.txt');

Rename To 

Rename a file or folder.

File::open('path\to\file.txt')->renameTo('file-1.txt');

Copy To 

Copy a file.

File::open('path\to\file.txt')->copyTo('path\to\file-2.txt');
File::open('path\to\file.txt')->copyTo('path\to\folder');
File::open('path\to\file.txt')->copyTo([
    'path\to\file-1.txt',
    'path\to\file-2.txt',
    'path\to\folder-1',
    'path\to\folder-2',
    …
]);

Don’t be affraid to write the same file name multiple times:

File::open('path\to\file.txt')->copyTo([
    'path\to\file-copy.txt',
    'path\to\file-copy.txt',
    'path\to\file-copy.txt',
    'path\to\file-copy.txt',
    'path\to\file-copy.txt',
    …
]);

Delete 

Delete a file or folder.

File::open('path\to\file.txt')->delete();
File::open('path\to\folder')->delete();

Get file modification time.

echo File::T('path\to\file.txt');

Consent 

Set file permission.

File::open('path\to\file.txt')->consent(0600);

Shortcut to set file permission during saving:

File::write('test!')->saveTo('path\to\file.txt', 0600);
File::open('path\to\file.txt')->write('test!')->save(0600);

Upload 

Upload a file.

File::upload($f, $path = ROOT, $fn = null, $fail = false, $c = []);

Example:

File::upload($_FILES['file'], 'path\to\folder');

Custom callback function:

File::upload($_FILES['file'], 'path\to\folder', function($file) {
    // do something…
});

Size 

Convert file size.

File::size($file, $unit = null, $prec = 2);

Options: B, KB, MB, GB, TB

echo File::size('path\to\file.txt'); // automatic (as short as possible)
echo File::size('path\to\file.txt', 'KB'); // force to `KB`
echo File::size(1000000, 'KB'); // as number(s) …

0 Comments

No comments yet.