I'm working on a file upload feature in CodeIgniter 3, but I'm encountering the following error:
File upload failed: The upload path does not appear to be valid.
Here are the details of the issue:
The upload folder is writable (chmod 777 applied), and the directory path is correct. I want to save the uploaded image in the database and display it in the view. Here is my controller code:
public function add_details()
{
$config['upload_path'] = FCPATH . 'uploads/';
$config['upload_path'] = './upload/';
$config['allowed_types'] = 'jpg|png|jpeg';
$config['max_size'] = 2048;
$this->load->library('upload', $config);
if (!$this->upload->do_upload('image')) {
$error = $this->upload->display_errors();
log_message('error', 'File upload failed: ' . $error);
$this->session->set_flashdata('error', 'File upload failed: ' . $error);
} else {
$imageData = $this->upload->data();
$record = [
'title' => $this->input->post('title'),
'description' => $this->input->post('description'),
'image' => base_url('uploads/' . $imageData['file_name']),
];
if ($this->DashboardModel->add_details($record)) {
$this->session->set_flashdata('success', 'Details added successfully.');
} else {
$this->session->set_flashdata('error', 'Failed to save details to the database.');
}
}
redirect(base_url('dashboard'));
}
}
I want to upload the image, save its path to the database, and display the uploaded image in the view.
upload(without an s) oruploads(with an s)? You're using both in the code.uploadsin this line, make sure to change that too:'image' => base_url('uploads/' . $imageData['file_name']),