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
PHP
echo form_open_upload('photos/upload');
echo form_label('Profile Picture');
echo form_file_select('profile_picture');
echo form_submit('submit', 'Upload');
echo form_close();
// Output:
// <form action="https://example.com/photos/upload" method="post" enctype="multipart/form-data">
// <label>Profile Picture</label>
// <input type="file" name="profile_picture">
// <button type="submit" name="submit">Upload</button>
// </form>Example #2: File Upload with Custom Attributes
PHP
$attributes = [
'id' => 'image-upload-form'
];
echo form_open_upload('gallery/process', $attributes);
// Output:
// <form action="https://example.com/gallery/process" method="post" enctype="multipart/form-data" id="image-upload-form">Example #3: Multiple File Uploads
PHP
echo form_open_upload('documents/batch_upload');
echo form_label('Upload Documents');
echo form_file_select('documents[]', ['multiple' => 'multiple']);
echo form_submit('submit', 'Upload Files');
echo form_close();
// Output includes the multiple attribute:
// <input type="file" name="documents[]" multiple="multiple">Example #4: Form with File Upload in a View
View File
<?php
echo form_open_upload('photos/submit');
echo form_label('Select File');
echo form_file_select('userfile');
echo form_submit('submit', 'Upload File');
echo form_close();
?>- 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)