
What You’ll Build
A complete image gallery application with:- User authentication (login required to upload)
- Image upload with validation
- Image storage using data URLs or file persistence
- Gallery display using cards
- Upload size and type restrictions
Database Schema
Create tables for images and user sessions.sqlpage/migrations/0001_images_table.sql
sqlpage/migrations/0002_users.sql
Building the Gallery View
index.sql
How It Works
Dynamic Menu
The shell menu changes based on authentication state - showing “login” or “logout” depending on whether a session cookie exists.
Card Display
Each image from the database is displayed as a card with:
top_image: The image URL shown at the top of the cardtitle: Image titledescription: Optional description text
Protected Upload Form
Only authenticated users can access the upload form.upload_form.sql
The
accept attribute restricts file selection to image types only. The browser will filter the file picker to show only images.
Handling File Uploads
upload.sql
File Storage Strategies
SQLPage provides two approaches for handling uploaded files:- File System Storage (Recommended)
- Database Storage (Data URLs)
Best for larger files and production useParameters:
'Image': The name of the file input field'images': Subdirectory to store files (relative to SQLPage root)'jpg,jpeg,png,gif': Allowed file extensions
/images/abc123.jpg)How It Works
How It Works
- SQLPage generates a random filename
- Validates the file extension against the allowed list
- Saves the file to
./images/directory - Returns the path to store in the database
- SQLPage automatically serves files from this directory
Advantages:
- Efficient for large files
- Minimal database load
- Files can be served directly by SQLPage or a CDN
- Easy to backup separately
File Upload Security
Authentication Check
Authentication Check
MIME Type Validation
MIME Type Validation
- Uploading executable files disguised as images
- Uploading documents or other non-image content
File Extension Whitelist
File Extension Whitelist
File Size Limits
File Size Limits
Configure in This sets the maximum upload size to 512KB (524,288 bytes). Adjust based on your needs.
sqlpage.json:File Upload Functions
sqlpage.uploaded_file_path
Returns the temporary path where the uploaded file is stored before processing.
sqlpage.uploaded_file_mime_type
Returns the MIME type of the uploaded file.
sqlpage.persist_uploaded_file
Saves the file to disk and returns the path.
sqlpage.read_file_as_data_url
Reads a file and converts it to a data URL for database storage.
Complete Application Flow
Project Structure
Configuration
sqlpage.json
Maximum upload size in bytes (default 10MB). Set to 5MB in this example.
Directory from which SQLPage serves static files. The
images/ folder should be within this directory.Enhancements
Image Thumbnails
Use
sqlpage.exec() to call ImageMagick or similar tools to generate thumbnails:Image Metadata
Store additional metadata like dimensions, file size, and upload user:
Delete Functionality
Allow users to delete their own uploads:
Image Categories
Add categories or tags for better organization:
Production Considerations
CDN Integration
For high-traffic sites, upload to S3/CDN using
sqlpage.exec() with AWS CLI or similar tools.Full Example
The complete working example with login functionality is available in the SQLPage repository.Default credentials: username:
admin / password: adminNext Steps
User Authentication
Deep dive into implementing secure authentication
File Upload Reference
Complete documentation of file handling functions