How to update pdf file and image file in codeigniter?

Member

by lizzie , in category: PHP Frameworks , 8 months ago

How to update pdf file and image file in codeigniter?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by lottie , 4 months ago

@lizzie 

To update a PDF file and an image file in CodeIgniter, you can use the following steps:

  1. Load the necessary libraries and helpers in your controller Make sure you have loaded the necessary libraries and helpers in your controller to handle file uploads. You may need to load the 'upload' library and 'form' helper for handling file uploads.
  2. Create a form in your view Create a form in your view that allows users to upload a PDF file and an image file. Make sure to set the form enctype attribute to 'multipart/form-data' to handle file uploads.
  3. Handle the file upload in your controller In your controller, check if a file has been uploaded and validate the file type and size. Use the 'upload' library to handle the file upload process. Save the uploaded files to a specific directory on the server.
  4. Update the file paths in your database After successfully uploading the files, update the file paths in your database with the new file paths for the PDF file and the image file.
  5. Display a Success message Once the files have been successfully updated, display a success message to the user.


Here is an example code snippet to update a PDF file and an image file in 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
public function update_files() {
    $config['upload_path'] = './uploads/';
    $config['allowed_types'] = 'pdf|jpeg|jpg|png';
    $this->load->library('upload', $config);
    
    if ($this->upload->do_upload('pdf_file')) {
        $pdf_data = $this->upload->data();
        $pdf_path = 'uploads/' . $pdf_data['file_name'];
    }
    
    if ($this->upload->do_upload('image_file')) {
        $image_data = $this->upload->data();
        $image_path = 'uploads/' . $image_data['file_name'];
    }
    
    // Update file paths in the database
    $this->your_model->update_files($pdf_path, $image_path);
    
    // Display success message
    $this->session->set_flashdata('success', 'Files updated successfully');
    
    redirect('your_controller/your_method');
}


View:

1
2
3
4
5
<?php echo form_open_multipart('your_controller/update_files'); ?>
    <input type="file" name="pdf_file">
    <input type="file" name="image_file">
    <button type="submit">Update Files</button>
<?php echo form_close(); ?>


This is just an example to get you started. Make sure to customize the code according to your project requirements and file structure.