form_open_upload()
function form_open_upload(string $location, array $attributes = []): string
Description
Generates the opening tag for an HTML form configured for file uploads. This function is identical to form_open() but automatically adds the enctype="multipart/form-data" attribute required for file submissions.
Important: Forms that accept file uploads must use form_open_upload() instead of form_open(). Without the proper encoding type, file data won't be transmitted to the server.
ParametersÂ
| Parameter | Type | Description |
|---|---|---|
| $location | string | The URL or path where the form will be submitted. |
| $attributes | array | (optional) HTML attributes for the form element. Defaults to an empty array ([]). |
Return Value
| Type | Description |
|---|---|
| string | The HTML opening tag for the form with enctype="multipart/form-data". |
Example #1: Basic File Upload Form
Example #2: File Upload with Custom Attributes
Example #3: Multiple File Uploads
Example #4: Form with File Upload in a View
- Always use
form_open_upload()for forms containingform_file_select()inputs. - The
enctype="multipart/form-data"attribute is automatically added and cannot be overridden. - File upload forms must use POST method (default).
- For security, always validate file types, sizes, and names server-side.
- Use
$_FILESsuperglobal to access uploaded file data in your controller. - Large file uploads may require adjusting PHP configuration (
upload_max_filesize,post_max_size). - For multiple file uploads, add
multiple="multiple"attribute to the file input and use array notation in the name (e.g.,name="files[]"). - Remember that
form_close()includes CSRF protection, which also applies to file upload forms.
Common Issues
If your $_FILES array is empty after form submission, check the following:
- You're using
form_open_upload()instead ofform_open() - The form method is POST (default)
- File size doesn't exceed
upload_max_filesizein php.ini - The total form data (including files) doesn't exceed
post_max_sizein php.ini - You're accessing the correct field name in
$_FILES(matches the name attribute of your file input)