0

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.

3
  • Is your upload directory called upload (without an s) or uploads (with an s)? You're using both in the code. Commented Jan 17 at 10:03
  • its uploads and i corrected the code by adding only $config['upload_path'] = './upload/'; but still the issue seem Commented Jan 20 at 4:38
  • You're also using uploads in this line, make sure to change that too: 'image' => base_url('uploads/' . $imageData['file_name']), Commented Jan 20 at 8:47

1 Answer 1

0

improve your code

public function add_details()
{
    $config['upload_path'] = './uploads/';
    $config['allowed_types'] = 'jpg|png|jpeg';
    $config['max_size'] = 2048;

    // Load upload library with the configuration
    $this->load->library('upload', $config);

    // Handle file upload
    if (!$this->upload->do_upload('image')) {
        // Log and flash error message
        $error = $this->upload->display_errors();
        log_message('error', 'File upload failed: ' . $error);
        $this->session->set_flashdata('error', 'File upload failed: ' . $error);
    } else {
        // Retrieve uploaded image data
        $imageData = $this->upload->data();

        // Prepare data for saving to the database
        $record = [
            'title' => $this->input->post('title'),
            'description' => $this->input->post('description'),
            'image' => 'uploads/' . $imageData['file_name'], // Store relative path in the database
        ];

        // Save to the database
        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 to dashboard
    redirect(base_url('dashboard'));
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.