0

I am trying to read the content of a .doc or .docx file we have uploaded.

I am using php with codeigniter, and I need to get the text from a file and save it in a database, so we can easily search it. I have tried every possible way but no luck so far.

Lucas Meine
  • 1,524
  • 4
  • 23
  • 32
Harshj
  • 1
  • 1
  • 'I have tried every possible way' - obviously not, like the comment above shows – Neo Feb 07 '20 at 11:38
  • Does this answer your question? [Extract text from doc and docx](https://stackoverflow.com/questions/5540886/extract-text-from-doc-and-docx) – Gabriel Solomon Feb 09 '20 at 13:02

1 Answers1

0

Once you get the uploaded file details parse the content of the file using these:

Read a doc/docx file in PHP: https://stackoverflow.com/a/19503654/3151134

PDF Parser library: https://github.com/smalot/pdfparser

This is a basic example on how to upload a file in CI, it's pretty straightforward:

View file in application/views/upload_form.php

<html>
[...]

<body>
    <?php echo $error;?>

    <?php echo form_open_multipart('upload/do_upload');?>
        <input type="file" name="userfile" />
        <input type="submit" value="upload" />
    </form>

</body>
</html>

Controller in application/controllers/Upload.php

<?php

class Upload extends CI_Controller {

        public function __construct()
        {
                parent::__construct();
                $this->load->helper(array('form', 'url'));
        }

        public function index()
        {
                $this->load->view('upload_form', array('error' => ' ' ));
        }

        public function do_upload()
        {
                $config['upload_path']          = './uploads/';
                $config['allowed_types']        = 'doc|docx|pdf';
                $config['max_size']             = 10000; //kilobytes

                // Load the Upload Library
                $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
                {

                        // Get the full path of the file
                        $fileFullPath = $this->upload->data('full_path');

                        /*** Parse the file and store its content in the db ***/

                        // After that maybe show a success page
                        $this->load->view('<nameofsuccesspageview>');
                }
        }
}
?>
Effe
  • 33
  • 1
  • 7