Mecha CMS

Mecha CMS blog and documentation.

Class File

Updated: Sunday, 07 August 2016

File operation.

Check the Existence of a File/Folder

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

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

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

Shortcut:

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

Create a File

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

Update a File

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

Append Text to File

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

Prepend Text to File

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

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(array(
    '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(array(
    '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 a File or Folder

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

Create a Folder

File::pocket('foo/bar', 0777);
File::pocket(array(
    'foo',
    'bar',
    'baz/qux'
), 0777);

Rename a File

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

Move a File or Folder

Added since version 1.0.2.

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');

Output the File Content

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

Reading File Line by Line

Added since version 1.1.3.

An alternative to the previous method which will allow you to stop the reading process on a specific line/condition:

// stop reading at line 4
echo File::open('path/to/file.txt')->get(4);
// stop reading at a line that contains `====`
echo File::open('path/to/file.txt')->get('====');

Upload a File

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

Custom callback function:

File::upload($_FILES['file'], 'path/to/folder', function($name, $type, $size, $link) {
    // Do something …
});

Convert File Size

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 numbers ...

Convert Private File Path to Public URL

Added since version 1.1.3.

echo File::url('.\www\lot\assets\file.txt');

Result:

http://mecha-cms.com/2016/lot/assets/file.txt

Convert Public URL to Private File Path

Added since version 1.1.3.

echo File::path('http://mecha-cms.com/2016/lot/assets/file.txt');

Result:

.\www\lot\assets\file.txt

Helper Methods

Added since version 1.1.4.

$path = 'foo\bar\baz\qux.txt';

// Get file base name
echo File::B($path); // → qux.txt
echo File::B($path, 2); // → baz\qux.txt

// Get file directory name
echo File::D($path); // → foo\bar\baz
echo File::D($path, 2); // → foo\bar

// Get string base name with custom separator → `.`
echo File::B('foo.bar.baz', 1, '.'); // → baz

// Get string directory name with custom separator → `.`
echo File::D('foo.bar.baz', 1, '.'); // → foo.bar

// Get file name without extension
echo File::N($path); // → qux

// Get file name with extension
echo File::N($path, true); // → qux.txt

// Get file extension
echo File::E($path, $fallback); // → txt

// Get file modification time
echo File::T($path, $fallback); // → 1448988019

Inspect File Details

Added since version 1.1.3.

$file = File::inspect('.\www\lot\assets\file.txt'));

Result for $file:

$file
 │
 ├── path: .\www\lot\assets\file.txt
 ├── name: file
 ├── url: http://mecha-cms.com/2016/lot/assets/file.txt
 ├── extension: txt
 ├── update_raw: 1403355917
 ├── update: 2014-06-21 20:05:17
 ├── size_raw: 21114
 ├── size: 20.62 KB
 └── is:
     ├── hidden: false
     ├── file: true
     └── folder: false

List All Files from a Folder

Added since version 1.1.3.

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

Results:

$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:
 │   ├── 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:
 │   └── path\to\folder\folder-2\file-1.txt: 1
 └── path\to\folder\folder-3:
     ├── 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:
         ├── 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

$tree_3
 │
 ├── 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

Options:

File::explore($folder, $recursive, $flat);
ParameterDescription
$folderThe folder path to explore.
$recursiveExplore the folder contents recursively.
$flatData output as flat array instead of nested array.

Set File Permission

Added since version 1.0.4.

File::open('path/to/file.txt')->setPermission(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);

Serialize and Unserialize

Added since version 1.1.0.

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

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

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

File::serialize('test!')->saveTo('path/to/file.txt');
Donation and Email Subscription