File Uploads
Core Forms supports file uploads with multi-layered security: extension blocklisting, MIME type validation, double extension detection, filename sanitization, and server-level execution prevention.
Basic Usage
Add a file input to your form HTML:

<p>
<label for="resume">Upload Resume</label>
<input type="file" name="resume" id="resume" required />
</p>
Core Forms automatically handles the upload, validation, and storage.
Multiple Files
Turn on Allow multiple files for a file field in the visual builder (File upload section), or Allow several files on the block editor’s File upload field. In code, add multiple; Core Forms appends [] to the name for you:
<p>
<label for="documents">Supporting documents</label>
<input type="file" name="documents[]" id="documents" multiple accept=".pdf,image/*" data-max-files="5" />
</p>
How it behaves for visitors:
- Each pick adds to the selection instead of replacing it, so visitors can collect files from different folders.
- Every chosen file is listed with its size and a Remove button.
- Picking the same file twice adds it once.
- Files over the limit, or of a type the field doesn’t allow, are left out with a short note under the list.
Without JavaScript, or in a browser that can’t edit a file selection, the field keeps the browser’s own behavior. The server applies the same rules either way.
Allowed File Types
Set Allowed file types (the accept attribute) to a comma-separated list of extensions and MIME types, such as .pdf,.docx or image/*. The builder has quick-add buttons for images, PDFs, documents and spreadsheets.
- The browser’s file picker shows only those types.
- A file the field doesn’t allow is refused on the server with the
file_invalid_extensionmessage, even when a visitor switches the picker to “All files”. - Extension spellings of one type count as the same type:
.jpgalso allows.jpeg. - Leave it blank to allow any type that passes the security checks below.
Maximum Files
Set Maximum files (the data-max-files attribute) once multiple files are on. Leave it blank for no limit.
- The file list shows “2 of 5 files” while visitors choose.
- A submission with more files is refused with the
file_too_manymessage. - PHP’s
max_file_uploads(often 20) still caps one submission; the builder shows this server’s value under the field.
A limit applies only to a field that sets it. Forms that repeat name="docs[]" across several single-file inputs keep working as before.
Security Layers
Only a file uploaded with the current request counts as a file. Text in a file field, such as a file name or a URL, is refused on forms, the headless API and automations, and is left out of drafts.
1. Blocked Extensions (30+)
The following extensions are rejected immediately:
php, php3, php4, php5, php7, php8, phtml, phar
exe, bat, cmd, com, msi
sh, bash, csh, ksh, zsh
cgi, pl, py, rb, jsp, asp, aspx
htaccess, htpasswd
svg, shtml, shtm
swf
2. Double Extension Detection
Filenames with dangerous inner extensions are blocked:
file.php.jpg -> BLOCKED (php in middle)
report.exe.pdf -> BLOCKED (exe in middle)
photo.png -> ALLOWED
data.backup.csv -> ALLOWED (backup is not dangerous)
3. MIME Type Validation
The actual file content is inspected using finfo to detect the real MIME type. If the detected MIME type does not match the file extension, the upload is rejected:
// file.jpg with actual content of a PHP script -> REJECTED
$detected_mime = Uploader::detect_mime_type( $filepath );
if ( ! Uploader::mime_matches_extension( $detected_mime, $extension ) ) {
return 'file_mime_mismatch';
}
Supported MIME Types
| Category | Extensions |
|---|---|
| Images | jpg, jpeg, jpe, png, gif, webp, avif, bmp, tif, tiff |
| Documents | pdf, doc, docx, xls, xlsx, ppt, pptx, odt, ods, rtf |
| Text | txt, log, csv |
| Archives | zip, rar, gz, 7z |
| Audio | mp3, wav, ogg, m4a |
| Video | mp4, webm, mov, avi |
4. Filename Sanitization
Uploaded filenames are cleaned before storage:
$safe = Uploader::sanitize_filename( $original );
// "My Report (final).pdf" -> "My-Report-final.pdf"
// "../../etc/passwd" -> "etcpasswd"
// "<script>hack</script>" -> "scripthackscript"
Rules applied:
- Remove dangerous characters:
< > : " / | ? *and control characters - Normalize whitespace to hyphens
- Collapse multiple hyphens
- Trim hyphens from edges
- Empty names default to
upload
5. Server-Level Protection
An .htaccess file is written to the upload directory to prevent script execution:
# Prevent script execution
<FilesMatch ".(?:php|phar|phtml|php[0-9]|cgi|pl|py|rb|sh|bash|asp|aspx|jsp)$">
Order allow,deny
Deny from all
</FilesMatch>
An index.php file prevents directory listing.
File Size Limit
Default maximum file size is 8 MB (8,000,000 bytes). Customize with the cf_upload_max_filesize filter:
// Allow up to 25 MB
add_filter( 'cf_upload_max_filesize', function() {
return 25 * 1024 * 1024; // 25 MB in bytes
} );
The 60 KiB submission-data ceiling applies only to encoded form values and file metadata. It does not count the uploaded binary. PHP’s upload_max_filesize, post_max_size, and max_file_uploads settings still apply to multipart requests alongside Core Forms’ per-file limit.
Multiple uploads using name="attachments[]" multiple are normalized and validated one file at a time. If form validation fails, pending uploads are discarded and temporary server paths are not stored with the submission.
Storage Location
Files are stored in:
wp-content/uploads/core-forms/{form-slug}/{form-slug}-{field-name}-{number}-{code}.{ext}
Example: wp-content/uploads/core-forms/contact/contact-resume-1-k3f9a2.pdf
- The number counts up for each form and field and never goes back down.
- The code is six random lowercase letters and digits, so upload URLs can’t be guessed by counting.
- Underscores in the field name become hyphens.
- Change the
{form-slug}-{field-name}part with thecf_upload_filename_prefixfilter.
Forms with Protected uploads turned on store new files outside the public web root instead. See Privacy and Retention.
Media Library Integration
Each upload gets a private WordPress attachment record, which links it to its form, field and entry. Turn that off for public uploads with:
add_filter( 'cf_upload_add_to_media', '__return_false' );
Uploads without an attachment record don’t appear on Core Forms > Media. Protected uploads always get one.
Form uploads are hidden from the WordPress Media Library and the editor’s media picker by default. Browse them on Core Forms > Media, a grid you can filter by form and file type, with links to each file and its submission. To list them in the Media Library too, choose Show form uploads under Core Forms > Settings > File Uploads. See Form Media.
Submission Data
After upload, the field data in the submission contains:
$submission->data['resume'] = [
'name' => 'contact-resume-1-k3f9a2.pdf',
'original_name' => 'My-Resume.pdf',
'url' => 'https://example.com/wp-content/uploads/core-forms/contact/contact-resume-1-k3f9a2.pdf',
'dir' => '/var/www/html/wp-content/uploads/core-forms/contact',
'size' => 245760,
'type' => 'application/pdf',
'attachment_id' => 567,
];
Error Messages
Custom error messages for file validation failures:
| Error Code | Default Message |
|---|---|
file_too_large | Uploaded file is too large. |
file_upload_error | An upload error occurred. Please try again later. |
file_not_uploaded |
Files must be uploaded. Please choose the file again. |
file_invalid_extension | This file type is not allowed. |
file_mime_mismatch | The uploaded file does not match its extension. Please upload a valid file. |
file_too_many | Too many files were added to one upload field. Please remove some and try again. |
Edit them per form under Messages > Other messages.
To change upload rules in code, filter cf_upload_field_rules. It receives field name => [ 'accept' => string, 'max_files' => int ] and the form:
// Allow up to 10 files in the "portfolio" field of every form.
add_filter( 'cf_upload_field_rules', function ( $rules ) {
if ( isset( $rules['portfolio'] ) ) {
$rules['portfolio']['max_files'] = 10;
}
return $rules;
} );