CodeIgniter Forums
print file content - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: print file content (/showthread.php?tid=17764)



print file content - El Forum - 04-15-2009

[eluser]Qiao[/eluser]
Hi guys!

I want to read file (af.txt near index.php) and echo it content.

controller af.php:
Code:
<?php
class Af extends Controller{
    function Af()
    {
        parent::Controller();
    }

    $this->load->helper('file');

    function index()
    {
        $content = read_file('af.txt');
        $this->load->view('af', $content);
    }
}
?>

view af.php:
Code:
<?=$content?>

The result is a blank page.
What am i doing wrong?


print file content - El Forum - 04-15-2009

[eluser]rogierb[/eluser]
Is this a typo?
$comtent versus $content


print file content - El Forum - 04-15-2009

[eluser]Qiao[/eluser]
Yes, it is typo. Corrected it.
But still blank page.

And if I add something static to viewer
<?=$content?>hhhhh
It is also blank. No any errors (so url is right).


print file content - El Forum - 04-15-2009

[eluser]rogierb[/eluser]
try to use
Code:
$content = read_file(FCPATH.'af.txt');
and add an file_exists() to see if the file exists. If not your path is wrong.


print file content - El Forum - 04-15-2009

[eluser]Qiao[/eluser]
I found mistakes:

1) $this->load->helper('file'); should be in the function, not out
2) only array should be passed to view, not variable

Code:
function index()
{
    $this->load->helper('file');
    $ar['content'] = read_file('af.txt');
    $this->load->view('af', $ar);
}

That is my first CI script!


print file content - El Forum - 04-15-2009

[eluser]Qiao[/eluser]
But i am wondering about helpers - why cannot i declare them out of the functions?
It would not be convenient to declare them in every function that use it.