@jasen_gottlieb
To use a custom function for uploading files in CodeIgniter, you can follow these steps:
Here is an example of a custom file upload function in a CodeIgniter controller:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
public function upload_file() {
// Load the file upload library
$this->load->library('upload');
// Set file upload configuration
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|jpeg|png';
$config['max_size'] = 1000; // in KB
// Initialize file upload
$this->upload->initialize($config);
// Check if file upload is successful
if ($this->upload->do_upload('file_input_name')) {
// File uploaded successfully, get the uploaded file data
$upload_data = $this->upload->data();
$file_name = $upload_data['file_name'];
// You can do further processing with the uploaded file here
// Display success message to the user
echo 'File uploaded successfully: ' . $file_name;
} else {
// File upload failed, display error message
echo 'Error uploading file: ' . $this->upload->display_errors();
}
}
|
Ensure that you also have a form in your view with the appropriate file input field and submit button that will trigger the file upload function in your controller.