4

I am trying to pull a random string from a .txt file on my server in order to display it in the footer of my site. Here is the code I am using (the first two lines are purely for debugging purposes):

<div id="footer">
    <div class="instructions">
        <?php
            $file = include_once 'facts.txt';
            echo dirname(__FILE__);
            $f_contents = file('facts.txt');
            $line = $f_contents[array_rand ($f_contents)];
            echo $line;
        ?>
    </div>
</div>

And here is what gets displayed in my footer:

TEST LINE - FOR TESTING PURPOSES.

/HOME/LEWIHTYV/PUBLIC_HTML/WP-CONTENT/THEMES/LEWNACY

WARNING: FILE(FACTS.TXT) [FUNCTION.FILE]: FAILED TO OPEN STREAM: NO SUCH FILE OR DIRECTORY IN /HOME/LEWIHTYV/PUBLIC_HTML/WP-CONTENT/THEMES/LEWNACY/FOOTER.PHP ON LINE 6

WARNING: ARRAY_RAND() EXPECTS PARAMETER 1 TO BE ARRAY, BOOLEAN GIVEN IN /HOME/LEWIHTYV/PUBLIC_HTML/WP-CONTENT/THEMES/LEWNACY/FOOTER.PHP ON LINE 7

For some reason, even through the file is being recognised when being included and the directory is correct (the footer.php and facts.txt files are both in the same directory), the random line selection is throwing this error. I have tested this code locally using WAMP and I have had no problems whatsoever. I have also checked the file permissions to make sure it is readable.

lewisj
  • 95
  • 1
  • 8
  • What is the output of: `echo $file;` ? – Rizier123 Jan 07 '15 at 22:34
  • 1
    You don't want to use `include once` here. Also if you are getting the directory save it in a variable. THen prepend the variable in the `file($dir . 'facts.txt');` – Elin Jan 07 '15 at 22:38
  • Why is your error all in caps? Anyways, it sounds like you are working with Linux (based on path in error) and Windows (WAMP). This can cause several wierd problems, but I would guess yours stems from: Windows is not case-sensitive, Linux is case-sensitive. Check that. – Dan Jan 07 '15 at 22:41
  • Also `include_once` will not error if the file is not found. Thats what `require_once` is for. – Dan Jan 07 '15 at 22:42
  • I should clarify I am using Windows - the error is only in all caps as I copied it straight from the rendered page (CSS text-transform: uppercase on that particular element). – lewisj Jan 07 '15 at 23:06
  • @Rizier123 `echo $file;` returns `1` – lewisj Jan 07 '15 at 23:07
  • @lewisj Then whats up with the file path? That doesn't look like a Windows path? – Dan Jan 07 '15 at 23:09
  • @dan08 Sorry, I misinterpreted what you were saying - the server is indeed Linux. – lewisj Jan 07 '15 at 23:13
  • One often runs into this error, and to quickly troubleshoot it, follow these steps : http://stackoverflow.com/a/36577021/2873507 – Vic Seedoubleyew Apr 12 '16 at 16:54

1 Answers1

1

The answer is PHP doesn't automatically assume that "facts.txt" is in the same directory as the PHP file that's running. You have to tell it the full path.

Try:

$f_contents = file(dirname(__FILE__) . '/facts.txt');
developerwjk
  • 8,619
  • 2
  • 17
  • 33