Alimin
Alimin

Reputation: 2727

How to read .txt or .doc file in codeigniter?

I am trying to follow this tutorial https://www.codeigniter.com/user_guide/helpers/file_helper.html and then I make this controller to display the file txt content which I call test.txt

class File_controller extends CI_Controller {
function __construct(){ 
parent::__construct();
        $this->load->helper('url'); 
            $this->load->helper('file');    
    }
public function index()
{
    $string = read_file(base_url().'test.txt');
    echo $string;
  }

 }

when I am testing this in browser no error is found, but the program is not displaying my file txt content,so

1.how to display test.txt correctly?

2.what the meaning is this parameter:

'./path/to/file.php'

when I am using

read_file('./path/to/file.php');

Upvotes: 0

Views: 14796

Answers (3)

Abdulla Nilam
Abdulla Nilam

Reputation: 38609

Get / Set the content of the file

Then using file_get_contents you can retrieve the content of the file.

and using file_put_contents you can set the content of the file.

You can also check

Upvotes: 0

Arcanyx
Arcanyx

Reputation: 870

As mentioned in the link https://ellislab.com/codeigniter/user-guide/helpers/file_helper.html

Note: The path is relative to your main site index.php file, NOT your controller or view files. CodeIgniter uses a front controller so paths are always relative to the main site index.

This means that

'./path/to/file.php'

will be the path in your directory relative to index.php Lets suppose your main site is in "mysite" folder. So the URL might be http://localhost/mysite/ and let us say the text file('test.txt') is in the main directory. So you will now access this path as

read_file('/mysite/test.txt');

Accordingly, the change in code.

Upvotes: 0

Mike Miller
Mike Miller

Reputation: 3129

First off edit your project index.php and set the environment to development so that errors are properly displayed. You do have an error its just suppressed with this change it will show you your errors. First one I can spot myself I think - the php function is actually readfile() not read_file(). For your use I think you will find file_get_contents() works better.

As regards your file path. As you have it your text file needs to be at the root of the project at the same level as your index.php file. It also will need to be readable. With error reporting in development mode you will get an error if any issues with path or permissions

Upvotes: 4

Related Questions