File Uploads
SQLPage makes it easy to accept file uploads from users and store them either on disk or in your database.Basic File Upload Form
Create a form with a file input field:Validating Uploaded Files
Always validate file uploads before processing:Storage Options
Option 1: Store Files on Disk
Usesqlpage.persist_uploaded_file() to save files to your web root:
'Document'- Form field name'uploads'- Target directory (relative to web root)'pdf,doc,docx'- Allowed file extensions
uploads/abc123.pdf)
Option 2: Store Files in Database
For small files, store them directly as base64-encoded data URLs:- Small images or icons
- Files that need to be embedded in pages
- Simplified backups (entire database contains all data)
Complete Image Gallery Example
Here’s a real-world example with authentication:File Upload Functions
sqlpage.uploaded_file_path(field_name)
Returns the temporary path where the uploaded file is stored.NULL if no file was uploaded for that field.
sqlpage.uploaded_file_mime_type(field_name)
Returns the MIME type of the uploaded file:sqlpage.persist_uploaded_file(field_name, directory, allowed_extensions)
Saves the uploaded file to the specified directory and returns the file path.- Generates a unique filename to prevent collisions
- Creates the target directory if it doesn’t exist
- Returns
NULLif the file extension is not allowed - The directory path is relative to your web root
sqlpage.read_file_as_data_url(file_path)
Reads a file and returns it as a base64-encoded data URL:Configuration
Maximum Upload Size
Set the maximum file size in yoursqlpage/sqlpage.json:
Security Best Practices
1. Always Validate File Types
Never trust file extensions alone - always check MIME types:2. Require Authentication
Always verify users are authenticated before accepting uploads:3. Sanitize Filenames
Thepersist_uploaded_file function automatically generates safe filenames, but if you need custom names:
4. Store Uploads Outside Web Root (Optional)
For maximum security, store uploaded files outside your web root and serve them through a controlled SQL endpoint:5. Set Appropriate Limits
Consider your use case when setting file size limits:- Profile pictures: 1-2 MB
- Documents: 10 MB
- Videos: 100+ MB (consider using external storage)
6. Scan for Malware
For production systems handling user uploads, consider integrating malware scanning:Displaying Uploaded Files
Images
Display uploaded images using thecard or table component:
Documents
Create download links:Advanced Techniques
Multiple File Uploads
Allow users to upload multiple files at once:Progress Indication
For large files, the browser shows a native upload progress indicator. SQLPage processes uploads efficiently in streaming mode.Image Resizing
SQLPage doesn’t include built-in image processing. For thumbnails, either:- Generate them client-side before upload using JavaScript
- Use database extensions (e.g., SQLite with image processing extensions)
- Process files with external tools using
sqlpage.exec()(requiresallow_exec = true)
Common Patterns
File Upload with Preview
Replace Existing File
Examples
See complete working examples:- Image gallery:
examples/image gallery with user uploads/ - Rich text editor with image uploads:
examples/rich-text-editor/