PHP code example of sghimire / mobile-file-access

1. Go to this page and download the library: Download sghimire/mobile-file-access library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

sghimire / mobile-file-access example snippets


use Sandip\FileAccess\Native\Facades\FileAccess;

// Save some bytes
FileAccess::save('report.pdf', $pdfBytes)
    ->mimeType('application/pdf')  // hint only — the file name's extension is what matters
    ->id('export-report')          // correlate this save with its events
    ->save();

// Open any file
FileAccess::pick()
    ->mimeTypes(['application/pdf'])  // restrict the picker, or omit for any file (default ['*/*'])
    ->id('import-report')
    ->pick();

use Sandip\FileAccess\Native\Events\FileAccess\FileSaved;
use Sandip\FileAccess\Native\Events\FileAccess\SaveCancelled;
use Sandip\FileAccess\Native\Events\FileAccess\FilePicked;
use Sandip\FileAccess\Native\Events\FileAccess\PickCancelled;
use Illuminate\Support\Facades\Event;

Event::listen(function (FileSaved $event) {
    $event->fileName; // string — the name the file was saved as
    $event->size;     // int — bytes written
    $event->id;       // ?string
});

Event::listen(function (SaveCancelled $event) {
    $event->reason; // ?string — "user_cancelled", "write_failed", ...
    $event->id;     // ?string
});

Event::listen(function (FilePicked $event) {
    $event->fileName;      // string
    $event->mimeType;      // string
    $event->size;          // int — bytes
    $event->contentBase64; // string — base64-encoded file contents
    $event->id;            // ?string
});

Event::listen(function (PickCancelled $event) {
    $event->reason; // ?string — "user_cancelled", "read_failed", ...
    $event->id;     // ?string
});

use Livewire\Component;
use Sandip\FileAccess\Native\Attributes\OnNative;
use Sandip\FileAccess\Native\Events\FileAccess\FilePicked;
use Sandip\FileAccess\Native\Facades\FileAccess;

class DocumentImporter extends Component
{
    public function startImport(): void
    {
        FileAccess::pick()->id('import')->mimeTypes(['application/pdf'])->pick();
    }

    #[OnNative(FilePicked::class)]
    public function onFilePicked(string $fileName, string $mimeType, int $size, string $contentBase64, ?string $id): void
    {
        file_put_contents(storage_path("app/imports/{$fileName}"), base64_decode($contentBase64));
    }
}
bash
php artisan native:plugin:register