Filesystem

Introduction

The Filesystem provides a comprehensive and intuitive interface for handling file operations. It is designed to simplify the process of interacting with the file system, making it easy to create, read, write, and check for the existence of files. Key features include:

File Creation: Easily create new files in the specified directory.

Existence Check: Verify whether a file already exists, preventing duplicate creations and potential overwrites.

Data Writing: Write data to files efficiently, supporting both string and binary data.

Data Reading: Read the contents of files into your application, with support for various data formats.

Use

Initialization

You can get an object of the Filesystem class by creating a new object

// app/Controllers/TestController.php
namespace App\Controllers;

use Ghosty\Framework\Filesystem\Filesystem;

class TestController
{
    public function show()
    {
        $Filesystem = new Filesystem();
    }
}

Also using DI

// app/Controllers/TestController.php
namespace App\Controllers;

use Ghosty\Framework\Filesystem\Filesystem;

class TestController
{
    public function __construct(private Filesystem $filesystem)
    {
        //
    }
}

Also using File Facade (Recommended)

// app/Controllers/TestController.php
namespace App\Controllers;

use Ghosty\Framework\Support\Facade\File;

class TestController
{
    public function show()
    {
        File::exists('./test.php');
    }
}

File exists

This method checks whether the specified file exists

// app/Controllers/TestController.php
namespace App\Controllers;

use Ghosty\Framework\Support\Facade\File;

class TestController
{
    public function show()
    {
        File::exists('./test.php');
    }
}

Put content

This method writes data to a file, if there is data in the file it will be replaced with new ones, if the file does not exist it will be created

// app/Controllers/TestController.php
namespace App\Controllers;

use Ghosty\Framework\Support\Facade\File;

class TestController
{
    public function show()
    {
        File::put('./test.php', 'Test content');
    }
}

Get content

This method returns the contents of the file as a string, if the file does not exist an exception will be thrown

// app/Controllers/TestController.php
namespace App\Controllers;

use Ghosty\Framework\Support\Facade\File;

class TestController
{
    public function show()
    {
        $content = File::get('./test.php');
    }
}

Append content

This method appends content to the end of a specific file, if the file does not exist it will be created

// app/Controllers/TestController.php
namespace App\Controllers;

use Ghosty\Framework\Support\Facade\File;

class TestController
{
    public function show()
    {
        File::append('./test.php', 'Content to append');
    }
}

Prepend content

This method appends content to the beginning of a specific file, if the file does not exist it will be created

// app/Controllers/TestController.php
namespace App\Controllers;

use Ghosty\Framework\Support\Facade\File;

class TestController
{
    public function show()
    {
        File::prepend('./test.php', 'Content to prepend');
    }
}