File fields
File storage is handled through file fields in your collections:File field options
MaxSelect
Controls whether the field stores a single file or multiple files:MaxSize
Specifies the maximum file size in bytes:If not set, the default maximum size is 5MB per file.
MimeTypes
Restricts allowed file types:Thumbs
Defines thumbnail sizes for image files:Protected
Controls file access:Even public files have randomized filenames (e.g.,
document_Ab3Kf9g2.pdf), providing obscurity. Protected files require a short-lived token for access.Uploading files
Single file upload
Multiple file upload
Modifying uploaded files
File modifiers
File fields support special modifier operations:File storage
Storage structure
Files are stored in the following hierarchy:Filename normalization
PocketBase automatically adds a random suffix to filenames:- Filename conflicts
- Guessing file URLs
- Accidental overwrites
Accessing files
File URLs
Public file URLs
For non-protected files:Protected file URLs
For protected files, generate a file token:Thumbnail URLs
Access image thumbnails:File validation
Automatic validation
File fields automatically validate:- File count (MaxSelect)
- File size (MaxSize)
- MIME type (MimeTypes)
- Filename format
Custom validation
File operations
Listing files
Finding files
Deleting files
Files are automatically deleted when:- Record is deleted
- File field value is updated (old files removed)
- File is explicitly removed using modifiers
S3 storage
PocketBase supports S3-compatible storage (AWS S3, MinIO, DigitalOcean Spaces, etc.):File hooks
Upload processing
Post-upload actions
File deletion
Direct filesystem access
Getting filesystem instance
File operations
List files
Performance optimization
Use appropriate file sizes
Use appropriate file sizes
Set reasonable MaxSize limits. Smaller files upload faster and consume less storage. Consider compressing large files client-side before upload.
Optimize thumbnail sizes
Optimize thumbnail sizes
Only generate thumbnails you actually use. Each thumbnail size requires storage space and processing time.
Enable CDN for public files
Enable CDN for public files
Use a CDN in front of your PocketBase instance to cache and serve files closer to users.
Lazy load images
Lazy load images
Load images on-demand rather than all at once. Use smaller thumbnails for previews and full-size images only when needed.
Use S3 for production
Use S3 for production
S3-compatible storage is generally more reliable and scalable than local filesystem storage for production deployments.
Implement file cleanup
Implement file cleanup
Regularly check for and remove orphaned files or old temporary files to save storage space.
Security best practices
Validate file types strictly
Validate file types strictly
Don’t rely solely on MIME types - they can be spoofed. Verify file contents match expected types.
Sanitize filenames
Sanitize filenames
PocketBase automatically sanitizes filenames, but be cautious when using user-provided filenames in other contexts.
Scan for malware
Scan for malware
Implement virus scanning for uploaded files, especially for user-facing applications.
Use protected files for sensitive data
Use protected files for sensitive data
Set
Protected: true for files containing sensitive information. This requires authentication to access.Implement rate limiting
Implement rate limiting
Limit file upload frequency and size per user to prevent abuse and resource exhaustion.
Set appropriate CORS headers
Set appropriate CORS headers
Configure CORS properly if files need to be accessed from different domains.