Skip to main content
NativePHP Desktop provides a fluent API for working with native file dialogs, allowing users to select files and directories or choose save locations.

Opening Files

Single File Selection

Open a dialog to select a single file:
use Native\Desktop\Facades\Dialog;

$filePath = Dialog::new()
    ->title('Select a file')
    ->open();

if ($filePath) {
    // User selected a file
    $contents = file_get_contents($filePath);
}

Multiple File Selection

Allow users to select multiple files:
$files = Dialog::new()
    ->title('Select files to import')
    ->multiple()
    ->open();

foreach ($files as $file) {
    // Process each file
}

Selecting Directories

Open a dialog to select a folder:
$directory = Dialog::new()
    ->title('Choose output directory')
    ->folders()
    ->open();
Allow multiple folder selection:
$directories = Dialog::new()
    ->folders()
    ->multiple()
    ->open();

File Filters

Restrict file selection to specific types:
$imagePath = Dialog::new()
    ->title('Select an image')
    ->filter('Images', ['jpg', 'png', 'gif', 'webp'])
    ->open();
Add multiple filters:
$filePath = Dialog::new()
    ->filter('Documents', ['pdf', 'doc', 'docx'])
    ->filter('Spreadsheets', ['xls', 'xlsx', 'csv'])
    ->filter('All Files', ['*'])
    ->open();

Save Dialogs

Open a save dialog to let users choose where to save a file:
$savePath = Dialog::new()
    ->title('Save Export')
    ->button('Save')
    ->defaultPath(storage_path('exports/data.csv'))
    ->save();

if ($savePath) {
    file_put_contents($savePath, $data);
}

Dialog Customization

Default Path

Set the initial directory or file path:
Dialog::new()
    ->defaultPath('/Users/username/Documents')
    ->open();

Custom Button Label

Change the default button text:
Dialog::new()
    ->button('Choose File')
    ->open();

Show Hidden Files

Allow selection of hidden files:
Dialog::new()
    ->withHiddenFiles()
    ->open();
Prevent automatic symlink resolution:
Dialog::new()
    ->dontResolveSymlinks()
    ->open();

Sheet Dialogs (macOS)

On macOS, display the dialog as a sheet attached to a window:
use Native\Desktop\Facades\Window;

$file = Dialog::new()
    ->asSheet() // Attaches to current window
    ->open();

// Or attach to a specific window
$file = Dialog::new()
    ->asSheet($windowId)
    ->open();
Sheet dialogs are modal to the window they’re attached to, while standalone dialogs are modal to the entire application.

Advanced Configuration

Custom Properties

Set custom dialog properties directly:
Dialog::new()
    ->properties([
        'openFile',
        'multiSelections',
        'showHiddenFiles'
    ])
    ->open();

Complete Examples

use Native\Desktop\Facades\Dialog;

class ImageImporter
{
    public function importImages()
    {
        $images = Dialog::new()
            ->title('Select Images to Import')
            ->filter('Images', ['jpg', 'jpeg', 'png', 'gif', 'webp'])
            ->multiple()
            ->defaultPath(storage_path('imports'))
            ->open();

        if (empty($images)) {
            return;
        }

        foreach ($images as $imagePath) {
            $this->processImage($imagePath);
        }
    }

    protected function processImage($path)
    {
        // Process the image
    }
}
Dialog methods return null if the user cancels the dialog. Always check the return value before proceeding.

Build docs developers (and LLMs) love