ZeroError Posted October 22, 2009 Share Posted October 22, 2009 I'm pretty new to PHP, and am currently trying to build a very simple homepage which randomises a quote in the middle of the page, but also randomises the colours in the <body> tag. I used a script from phpjunkyard.com to do the randomising and just require the scripts into the webpage. I used this code for the body tag: <body <?php require '/random/rand_bodycolour.php'; ?> > and this code for the main quote: <?php require '/random/rand_homebody.php'; ?> and receive this error: Warning: require(/random/rand_bodycolour.php) [function.require]: failed to open stream: No such file or directory in /home/user/public_html/index.php on line 10 Fatal error: require() [function.require]: Failed opening required '/random/rand_bodycolour.php' (include_path='.:/usr/lib/php:/usr/local/lib/php') in /home/user/public_html/index.php on line 10 I had a brief problem when I moved the two files, which I fixed, but this error still occurs. I must have checked it at least six times, and cannot see anything wrong with it. Any ideas? I did a brief Google search, and a quick search of your archives and couldn't find anything relating to my exact problem. Quote Link to comment https://forums.phpfreaks.com/topic/178618-solved-require-reports-fatal-error-althought-the-file-exists/ Share on other sites More sharing options...
PFMaBiSmAd Posted October 22, 2009 Share Posted October 22, 2009 Leading slashes / on file paths refer to the root of the current disk. I'll guess that the random folder is actually in your document root folder? If so, you should form a reference to the document root folder as follows - require $_SERVER['DOCUMENT_ROOT'] . '/random/rand_bodycolour.php'; Quote Link to comment https://forums.phpfreaks.com/topic/178618-solved-require-reports-fatal-error-althought-the-file-exists/#findComment-942100 Share on other sites More sharing options...
ZeroError Posted October 22, 2009 Author Share Posted October 22, 2009 Thank you, that seems to have worked. But now I get this error: Warning: array_rand() [function.array-rand]: First argument has to be an array in /home/kittycat/public_html/random/rand_bodycolour.php on line 56 This is line 56 in the script: 54. if (count($settings['quotes'])) 55. { 56. $txt = $settings['quotes'][array_rand($settings['quotes'])]; 57. } Quote Link to comment https://forums.phpfreaks.com/topic/178618-solved-require-reports-fatal-error-althought-the-file-exists/#findComment-942182 Share on other sites More sharing options...
Daniel0 Posted October 22, 2009 Share Posted October 22, 2009 Well, have you checked that $settings['quotes'] actually is an array? Quote Link to comment https://forums.phpfreaks.com/topic/178618-solved-require-reports-fatal-error-althought-the-file-exists/#findComment-942183 Share on other sites More sharing options...
ZeroError Posted October 22, 2009 Author Share Posted October 22, 2009 Hmm, there is a line saying $settings['quotes'] = file($settings['text_from_file']); Does that qualify? Or should $settings['quotes'] be set to an array, with its contents being from the file 'text_from_file'? Quote Link to comment https://forums.phpfreaks.com/topic/178618-solved-require-reports-fatal-error-althought-the-file-exists/#findComment-942188 Share on other sites More sharing options...
Daniel0 Posted October 22, 2009 Share Posted October 22, 2009 Hmm... in that case it should be an array. Try var_dump'ing it within the if just to make sure. Quote Link to comment https://forums.phpfreaks.com/topic/178618-solved-require-reports-fatal-error-althought-the-file-exists/#findComment-942191 Share on other sites More sharing options...
ZeroError Posted October 22, 2009 Author Share Posted October 22, 2009 Do you mean line 56, or the array declaration? Quote Link to comment https://forums.phpfreaks.com/topic/178618-solved-require-reports-fatal-error-althought-the-file-exists/#findComment-942205 Share on other sites More sharing options...
Daniel0 Posted October 22, 2009 Share Posted October 22, 2009 Yeah just before l. 56, try putting var_dump($settings['quotes']) to see what it actually contains. Quote Link to comment https://forums.phpfreaks.com/topic/178618-solved-require-reports-fatal-error-althought-the-file-exists/#findComment-942209 Share on other sites More sharing options...
ZeroError Posted October 22, 2009 Author Share Posted October 22, 2009 bool(false) That doesn't seem like much...? Quote Link to comment https://forums.phpfreaks.com/topic/178618-solved-require-reports-fatal-error-althought-the-file-exists/#findComment-942212 Share on other sites More sharing options...
Daniel0 Posted October 22, 2009 Share Posted October 22, 2009 Then it must've somehow been unset or it's out of scope. Try tracking the value from where it's defined up until that line. Quote Link to comment https://forums.phpfreaks.com/topic/178618-solved-require-reports-fatal-error-althought-the-file-exists/#findComment-942213 Share on other sites More sharing options...
ZeroError Posted October 22, 2009 Author Share Posted October 22, 2009 Sorry, what do you mean by "out of scope"? It doesn't seem to have been unset in anyway, and I've commented out a section that offered an "alternative way of using quotes" that didn't involve the file and used the same array. Quote Link to comment https://forums.phpfreaks.com/topic/178618-solved-require-reports-fatal-error-althought-the-file-exists/#findComment-942218 Share on other sites More sharing options...
Daniel0 Posted October 22, 2009 Share Posted October 22, 2009 The scope is the area where some sort of action has effect: http://dk.php.net/manual/en/language.variables.scope.php Quote Link to comment https://forums.phpfreaks.com/topic/178618-solved-require-reports-fatal-error-althought-the-file-exists/#findComment-942219 Share on other sites More sharing options...
ZeroError Posted October 22, 2009 Author Share Posted October 22, 2009 Ah, cr*p. I've been a complete idiot and discounted the fact that the file (text_from_file), for whatever reason, could not be found. I used the solution given earlier for that, and it fixed both issues. Sorry for wasting your time, and thank you for your help! Quote Link to comment https://forums.phpfreaks.com/topic/178618-solved-require-reports-fatal-error-althought-the-file-exists/#findComment-942231 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.