Working with File Systems and Storage in NativePHP


One of the best parts of building desktop apps with NativePHP is that you gain full access to the user’s local file system — directly from your Laravel code. Whether you want to read a file, save user data, or work with documents and media, NativePHP lets you do it the Laravel way.

In this article, we’ll explore how to:

  • Access and manipulate the file system

  • Read and write local files

  • Integrate with Laravel’s storage API

  • Use dialogs for selecting and saving files


📁 Why File Access Matters in Desktop Apps

Unlike web apps, desktop applications are expected to interact with the local file system. Think about:

  • Image or document editors

  • PDF generators and readers

  • Media players

  • Backup tools

  • Data import/export apps

NativePHP enables all of these scenarios using Laravel’s familiar filesystem syntax.


🔧 Setting Up Storage in NativePHP

Laravel comes preconfigured with a powerful file storage system. In config/filesystems.php, you can define local or cloud disks. For NativePHP, the local driver is your go-to for interacting with files on the user’s machine.

Example setup:

'disks' => [
    'native' => [
        'driver' => 'local',
        'root' => storage_path('app/native'),
    ],
],

📄 Writing Files

Let’s say you want to save some text into a file on the user’s system:

use Illuminate\Support\Facades\Storage;

Storage::disk('native')->put('notes.txt', 'These are saved locally.');

This will create a notes.txt file in the storage/app/native directory.


📖 Reading Files

You can also retrieve file contents just like you would in any Laravel app:

$content = Storage::disk('native')->get('notes.txt');

Need to check if a file exists before reading?

if (Storage::disk('native')->exists('notes.txt')) {
    $data = Storage::disk('native')->get('notes.txt');
}

🗑 Deleting Files

Removing files is simple:

Storage::disk('native')->delete('notes.txt');

📂 Letting Users Choose Files or Folders

You can combine file system access with NativePHP dialogs to let users choose paths dynamically.

Open a file:

use Native\Laravel\Facades\Dialog;

Dialog::openFile()
    ->title('Open File')
    ->then(function ($filePath) {
        $content = file_get_contents($filePath);
    });

Save a file:

Dialog::saveFile()
    ->title('Save File')
    ->defaultPath('report.txt')
    ->then(function ($filePath) {
        file_put_contents($filePath, 'Report content here.');
    });

Open a folder:

Dialog::openDirectory()
    ->title('Choose Folder')
    ->then(function ($folderPath) {
        // You can now write to this folder or list its contents
    });

💡 Tips & Best Practices

  • Permissions: Desktop apps have full file access but be respectful. Always confirm before writing or deleting.

  • User Experience: Use dialogs instead of hardcoded paths to give users control over where data is stored or retrieved.

  • File Types: Use filters (e.g., .csv, .json) when opening/saving files to enhance usability.

  • Cross-Platform Paths: Use Laravel's helper functions (storage_path(), base_path()) to build safe paths.


🔚 Wrapping Up

With NativePHP, accessing the user’s file system feels just like working with Laravel on the backend — no extra APIs, no new syntax. This makes your code more maintainable and easier to debug, while still giving you full desktop-level power.