Search code examples
phpreadfile

readfile not working with concatenation


1. I am using readfile to print the contents of a file:

<?php readfile ('osta/css/themes/selected.txt'); ?>

2. This works perfectly. But I now need to use ROOT_PATH. I add it like so:

<?php readfile (ROOT_PATH . 'osta/css/themes/selected.txt'); ?>

But nothing is printed.

3. To verify that I have the correct path I try this:

<a href="<?php echo (ROOT_PATH . 'osta/css/themes/selected.txt'); ?>">test</a>

which produces a working link to the file.

What am I doing wrong in step #2?


Solution

  • Since ROOT_PATH works in href, that means it's a pathname relative to the webserver's document root, not the root of the real filesystem. readfile() expects a pathname in the real filesystem, so you need to add the document root.

    <?php readfile ($_SERVER['DOCUMENT_ROOT'] . ROOT_PATH . 'osta/css/themes/selected.txt'); ?>