Chunked File Uploads
Large files (videos, desktop installers, archives) often hit PHP upload_max_filesize, Nginx client_max_body_size, or Cloudflare's 100 MB request body cap.
v2.0 includes native chunked file uploads out of the box:
- Zero manual routes —
CrudServiceProviderautomatically registersPOST /crud/chunk-uploadandDELETE /crud/chunk-upload/{uploadId}. - Field-driven configuration — Control chunk size, max file size, accepted extensions, disk, directory, and column mappings straight from
resourceConfig()['fields']. - Signed server enforcement — Field limits (
max_size_mb,accept) are encrypted per form render and enforced server-side on every slice. - Zero-boilerplate persistence —
CrudControllervalidates tokens, streams files to disk, resolves edit-modeexisting_filepreviews, and deletes replaced/removed files automatically.
Global Defaults (config/crud.php)
'chunk_upload' => [
'enabled' => true,
'route' => '/crud/chunk-upload',
'route_name' => 'crud.chunk-upload',
'middleware' => ['web', 'auth'],
'disk' => 'public',
'temp_directory' => 'crud-chunks',
'chunk_size_mb' => 5,
'max_chunk_size_mb' => 25,
'max_file_size_mb' => 512,
'max_retries' => 3,
'ttl_hours' => 6,
'allowed_extensions' => [],
],Custom Auth Guard
If your dashboard uses a named guard (e.g. auth:web with verified), update chunk_upload.middleware in config/crud.php:
'middleware' => ['web', 'auth:web', 'verified'],Usage in resourceConfig()
'fields' => [
[
'key' => 'video_path',
'label' => 'Performance Video',
'type' => 'chunked-file',
'rules' => 'required',
'accept' => '.mp4,.mov,.webm',
'chunk_size_mb' => 8,
'max_size_mb' => 300,
'disk' => 'public',
'directory' => 'recordings',
],
],'fields' => [
[
'key' => 'mac_file',
'label' => 'macOS Installer (.dmg, .zip, .pkg)',
'type' => 'chunked-file',
'rules' => 'nullable',
'accept' => '.dmg,.zip,.pkg',
'chunk_size_mb' => 8,
'max_size_mb' => 500,
'max_retries' => 3,
'disk' => 'public',
'directory' => fn ($model, $data) => 'releases/' . ($data['version'] ?? $model->version ?? 'v1'),
'preserve_filename' => true,
'delete_old' => true,
'path_column' => 'mac_file_path',
'name_column' => 'mac_file_name',
'size_column' => 'mac_file_size',
'url_column' => 'mac_url',
],
],How Lifecycle Persistence Works
- Create / Edit render:
CrudService::prepareFieldsForFrontend()strips server-only closures (directory,filename), injects the native endpoint, chunk limits, a signedupload_configtoken, and (on edit) theexisting_filemetadata (path,name,size,url). - Client slicing:
ChunkedFileInputvalidates extension and file size upfront, slices the file intochunk_size_mbpieces, retries transient/5xx failures up tomax_retriestimes, and disables form submission while any chunked field is uploading (tracked per field key so multiple concurrent uploads work safely). - Server assembly:
ChunkUploadControllerverifies the signed config, stores each slice understorage/app/crud-chunks/uploads/{uploadId}, usesflock()for concurrency-safe reassembly, checks assembled byte count againstfile_size, writes a user-scoped.meta.jsonsidecar, and returns{ completed: true, token, name, size }. - Form submit (
store/update):ChunkedFileRuleverifies token validity & user ownership (and ensures'required'passes on edit when keeping an existing file).ChunkUploadService::processField()then streams the temp file to the targetdisk/directory, setspath_column/name_column/size_column, deletes old files if replaced or removed ({ removed: true }), and deletes the temp file. - Model deletion (
destroy/bulkDestroy): Whendelete_old => trueand the model does not useSoftDeletes, associated files are automatically deleted from disk.
Field Options Reference
| Key | Type | Default | Description |
|---|---|---|---|
accept | string|null | null | Comma-separated extensions or MIME types (validated client & server-side) |
chunk_size_mb | int | config('crud.chunk_upload.chunk_size_mb') | Slice size in MB |
max_size_mb | int | config('crud.chunk_upload.max_file_size_mb') | Max total file size in MB |
max_retries | int | config('crud.chunk_upload.max_retries') | Retry attempts per slice on network/5xx errors |
endpoint | string|null | Native /crud/chunk-upload | Optional custom endpoint override |
disk | string | config('crud.chunk_upload.disk') | Target Laravel filesystem disk |
directory | string|Closure | 'uploads' | Destination path (fn($model, $data) => ... supported) |
filename | Closure|null | null | Custom filename callback fn($originalName, $model, $data) |
preserve_filename | bool | false | Keep sanitized original name (false appends a random suffix) |
visibility | string|null | null | Optional 'public' or 'private' visibility |
delete_old | bool | true | Delete old file on replace, remove, or permanent destroy |
path_column | string | $field['key'] | Model attribute storing the saved path |
name_column | string|null | null | Optional attribute storing original filename |
size_column | string|null | null | Optional attribute storing byte size |
url_column | string|null | null | Optional attribute/accessor for download link (fallback: Storage::disk($disk)->url($path)) |
auto_handle | bool | true | Set false to manually resolve tokens inside beforeSave() |
Standalone Usage (Custom Forms & Modals)
Use ChunkedFileInput and ChunkUploadService directly on custom Inertia pages or modals:
import ChunkedFileInput from '@/components/crud/ChunkedFileInput';
<ChunkedFileInput
accept=".mp4,.webm,.mov"
chunkSizeMB={8}
maxSizeMB={500}
onChange={(val) => {
setVideoToken(val && 'token' in val ? val.token : null);
}}
onUploadingChange={setIsUploading}
/>use NagibMahfuj\Crud\Services\ChunkUploadService;
public function attachVideo(Request $request, ChunkUploadService $chunkUploads)
{
$resolved = $chunkUploads->resolveToken((string) $request->input('video_token'));
if (!$resolved) {
return response()->json(['message' => 'Upload token invalid or expired.'], 422);
}
// $resolved = ['token', 'path', 'meta_path', 'name', 'size', 'mime_type']
Storage::disk('public')->putFileAs('recordings', new \Illuminate\Http\File($resolved['path']), 'final.mp4');
@unlink($resolved['path']);
}Housekeeping & Stale Upload Cleanup
Partial slices are deleted immediately when a user clicks Cancel (DELETE /crud/chunk-upload/{uploadId}), and expired uploads older than ttl_hours are pruned opportunistically after completed merges.
For busy apps, schedule the built-in cleanup command in routes/console.php:
use Illuminate\Support\Facades\Schedule;
Schedule::command('crud:cleanup-chunks')->hourly();You can also run it manually with a custom retention window:
php artisan crud:cleanup-chunks --hours=2