@elisha_langworth
To upload an image in CodeIgniter, you will need to follow these steps:
1 2 3 4 5 |
<form method="post" action="<?php echo base_url('upload/do_upload'); ?>" enctype="multipart/form-data"> <input type="file" name="userfile" size="20" /> <br /><br /> <input type="submit" value="upload" /> </form> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
public function do_upload() { $config['upload_path'] = './uploads/'; $config['allowed_types'] = 'gif|jpg|png'; $config['max_size'] = 100; $config['max_width'] = 1024; $config['max_height'] = 768; $this->load->library('upload', $config); if ( ! $this->upload->do_upload('userfile')) { $error = array('error' => $this->upload->display_errors()); $this->load->view('upload_form', $error); } else { $data = array('upload_data' => $this->upload->data()); $this->load->view('upload_success', $data); } } |
@elisha_langworth
To upload an image in CodeIgniter, you can follow these steps:
1 2 3 4 |
|
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 28 29 30 31 32 33 34 35 36 |
// controllers/Image.php class Image extends CI_Controller { public function upload_image() { // Check if file is selected for upload if (isset($_FILES['userfile']['name'])) { $config['upload_path'] = './uploads/'; $config['allowed_types'] = 'gif|jpg|png'; $config['max_size'] = 2048; // 2MB $this->load->library('upload', $config); if (!$this->upload->do_upload('userfile')) { // If upload fails, redirect to error page or show error message $error = array('error' => $this->upload->display_errors()); $this->load->view('upload_form', $error); } else { // If upload is successful, retrieve file data $data = $this->upload->data(); // Your further processing logic here, save file to database, etc. // ... // Redirect to success page or show success message redirect('image/upload_success'); } } // If no file is selected for upload, redirect to form page redirect('image'); } public function upload_success() { $this->load->view('upload_success'); } } |
1
|
Image uploaded successfully! |
That's it! Now, when you submit the form, the selected image will be uploaded to the uploads
folder and the user will be redirected to the upload_success
page.