1. Go to this page and download the library: Download entoknakal/file-picker 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/ */
entoknakal / file-picker example snippets
namespace App\NativeComponents;
use entoknakal\FilePicker\Events\FilePickerCopy;
use entoknakal\FilePicker\Facades\FilePicker;
use Illuminate\Support\Facades\Storage;
use Illuminate\View\View;
use Native\Mobile\Edge\NativeComponent;
class PickFile extends NativeComponent
{
public array $allowedTypes = ['application/pdf', 'photos'];
// Set to true for multiple selection. Default value is false.
public bool $multiple = false;
public array $selectedFiles = [];
// First file metadata shortcuts (for single file compatibility)
public ?string $filePath = null;
public ?string $fileName = null;
public ?string $mimeType = null;
public ?string $fileSize = null;
public ?string $extension = null;
public ?string $imagesFilePath = null;
public ?string $allowedTypesString = null;
public ?bool $status = null; // Default state is null
public ?string $errorMessage = null;
public function selectFile(): void
{
$this->errorMessage = null;
$this->status = null;
$this->selectedFiles = [];
try {
$fileData = FilePicker::open([
'types' => $this->allowedTypes,
'multiple' => $this->multiple,
]);
if (empty($fileData)) {
return;
}
if (isset($fileData['error'])) {
$this->status = false;
$this->errorMessage = $fileData['error'];
return;
}
// Normalize payload structure
if (isset($fileData['files']) && is_array($fileData['files'])) {
$items = $fileData['files'];
} elseif (isset($fileData) && is_array($fileData)) {
$items = $fileData;
} else {
$items = [$fileData];
}
$disk = Storage::disk('mobile_public');
$this->allowedTypesString = implode(', ', $this->allowedTypes);
foreach ($items as $index => $item) {
if (empty($item['path']) || !file_exists($item['path'])) {
continue;
}
$originalName = $item['name'] ?? ('file_' . time());
$ext = strtolower(pathinfo($originalName, PATHINFO_EXTENSION));
$baseName = pathinfo($originalName, PATHINFO_FILENAME);
$fileName = $baseName . ($ext ? '.' . $ext : '');
// Stream copy from temporary cache
FilePickerCopy::dispatch(
$item['path'],
$fileName,
'ztssUpload',
'mobile_public'
);
$relativePath = 'ztssUpload/' . $fileName;
$finalPath = $disk->path($relativePath);
if (!file_exists($finalPath)) {
if (!$disk->exists('ztssUpload')) {
$disk->makeDirectory('ztssUpload');
}
copy($item['path'], $finalPath);
@unlink($item['path']);
}
if (!file_exists($finalPath)) {
continue;
}
$mimeType = $item['mime_type'] ?? null;
$isImage = in_array($ext, ['jpg', 'jpeg', 'png', 'webp', 'gif'])
|| ($mimeType && str_starts_with($mimeType, 'image/'));
$fileSrc = str_starts_with($finalPath, 'file://') ? $finalPath : 'file://' . $finalPath;
$this->selectedFiles[] = [
'name' => $fileName,
'path' => $finalPath,
'file_src' => $fileSrc,
'extension' => $ext,
'mime_type' => $mimeType,
'size' => $item['size'] ?? (file_exists($finalPath) ? filesize($finalPath) : 0),
'is_image' => $isImage,
];
}
if (!empty($this->selectedFiles)) {
$this->status = true;
$first = $this->selectedFiles[0];
$this->fileName = $first['name'];
$this->filePath = $first['path'];
$this->extension = $first['extension'];
$this->mimeType = $first['mime_type'];
$this->fileSize = (string) $first['size'];
$this->imagesFilePath = $first['file_src'];
} else {
$this->status = false;
$this->errorMessage = 'Failed to save files into the ztssUpload directory.';
}
} catch (\Throwable $e) {
$this->status = false;
$this->filePath = null;
$this->selectedFiles = [];
$this->errorMessage = $e->getMessage();
}
}
public function render(): View
{
return view('native.pick-file');
}
}
use entoknakal\FilePicker\Events\FilePickerCopy;
FilePickerCopy::dispatch(
sourcePath: $item['path'], // Absolute Android system cache temporary path
originalName: $fileName, // Desired target filename (e.g., "document.pdf")
targetFolder: 'ztssUpload', // Subfolder directory name
disk: 'mobile_public' // Configured Laravel Storage Disk name
);
public array $allowedTypes = ['photos']; // Resolves to image/*
public array $allowedTypes = ['image/png', 'application/pdf', 'application/octet-stream'];