RudeYute Posted May 1, 2011 Share Posted May 1, 2011 Hi, Please bear with me, I have no real experience with PHP but am using it for the first time in a page for maintenance purposes. One of the things I am trying to do is include a random page from within a certain folder (folder: 'modules/did-you-know'). Inside this folder there are currently three files named in this format 'did-you-know-###.php' - where ### is the page number. I have no problems including the named page individually, but it is when randomising it where it causes errors. The code I am using is the following: <?php $i=0; $myDirectory = dir("modules/did-you-know"); while($file=$myDirectory->read()) { $array[$i]=$file; $i++; } $myDirectory->close(); $num = count($array); $random = rand(0, $num); include "$array[$random]"; ?> When I load the page, I get numerous errors, as follows: Warning: include(..) [function.include]: failed to open stream: Operation not permitted in /home/{USERNAME}/public_html/test2/index.php on line 103 Warning: include(..) [function.include]: failed to open stream: Operation not permitted in /home/{USERNAME}/public_html/test2/index.php on line 103 Warning: include() [function.include]: Failed opening '..' for inclusion (include_path='.:/usr/lib/php:/usr/local/lib/php') in /home/{USERNAME}/public_html/test2/index.php on line 103 Warning: include(did-you-know-002.php) [function.include]: failed to open stream: No such file or directory in /home/{USERNAME}/public_html/test2/index.php on line 103 Warning: include(did-you-know-002.php) [function.include]: failed to open stream: No such file or directory in /home/{USERNAME}/public_html/test2/index.php on line 103 Warning: include() [function.include]: Failed opening 'did-you-know-002.php' for inclusion (include_path='.:/usr/lib/php:/usr/local/lib/php') in /home/{USERNAME}/public_html/test2/index.php on line 103 Now, it's clear from the above that it is (at least sometimes) reading the files from within the 'did-you-know' directory, so not sure why I am getting these errors. (Especially the last one, as that would suggest an error to do with permissions, would it not?) Line 103 is the "include" line. Any help would be appreciated. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/235283-include-random-page-from-folder-errors/ Share on other sites More sharing options...
wildteen88 Posted May 1, 2011 Share Posted May 1, 2011 You need to add modules/did-you-know to your include path $file = $array[$random]; include "modules/did-you-know/$file"; Quote Link to comment https://forums.phpfreaks.com/topic/235283-include-random-page-from-folder-errors/#findComment-1209069 Share on other sites More sharing options...
RudeYute Posted May 1, 2011 Author Share Posted May 1, 2011 Hmm, I have now changed it to the following: <?php $i=0; $myDirectory = dir("modules/did-you-know"); while($file=$myDirectory->read()) { $array[$i]=$file; $i++; } $myDirectory->close(); $num = count($array); $random = rand(0, $num); $file = $array[$random]; include "modules/did-you-know/$file"; ?> And it works sometimes, but sometimes gives me the following error (line 104 is the includes line again) Warning: include(modules/did-you-know/..) [function.include]: failed to open stream: No such file or directory in /home/uocopstt/public_html/test2/index.php on line 104 Warning: include(modules/did-you-know/..) [function.include]: failed to open stream: No such file or directory in /home/uocopstt/public_html/test2/index.php on line 104 Warning: include() [function.include]: Failed opening 'modules/did-you-know/..' for inclusion (include_path='.:/usr/lib/php:/usr/local/lib/php') in /home/uocopstt/public_html/test2/index.php on line 104 Thanks Quote Link to comment https://forums.phpfreaks.com/topic/235283-include-random-page-from-folder-errors/#findComment-1209076 Share on other sites More sharing options...
wildteen88 Posted May 1, 2011 Share Posted May 1, 2011 It helps if you stop censoring your error messages. What file path does it error out on? Quote Link to comment https://forums.phpfreaks.com/topic/235283-include-random-page-from-folder-errors/#findComment-1209079 Share on other sites More sharing options...
RudeYute Posted May 1, 2011 Author Share Posted May 1, 2011 I didn't censor it that time (it slipped my mind lol) - That is the exact error message it displayed, which seemed strange to me too. But the fact that it displays the page sometimes, but error messages other times suggests it is reading at least some of the files. EDIT: It sometimes displays one dot (.), sometimes two (..) and sometimes none at all as the filename. What does that mean? Quote Link to comment https://forums.phpfreaks.com/topic/235283-include-random-page-from-folder-errors/#findComment-1209084 Share on other sites More sharing options...
wildteen88 Posted May 1, 2011 Share Posted May 1, 2011 Oh, hangon. I see what it is doing now. Sorry about that. It is because dir() is returning . and .. into your array. These periods have special meanings in file paths. You need to filter the results that dir() returns. Change the while loop to while($file=$myDirectory->read()) { if($file != '.' && $file != '..') { $array[$i]=$file; $i++; } } Quote Link to comment https://forums.phpfreaks.com/topic/235283-include-random-page-from-folder-errors/#findComment-1209087 Share on other sites More sharing options...
RudeYute Posted May 1, 2011 Author Share Posted May 1, 2011 Not sure if that helped or not, still get the following errors: Warning: include(modules/did-you-know/) [function.include]: failed to open stream: No such file or directory in /home/uocopstt/public_html/test2/index.php on line 107 Warning: include(modules/did-you-know/) [function.include]: failed to open stream: No such file or directory in /home/uocopstt/public_html/test2/index.php on line 107 Warning: include() [function.include]: Failed opening 'modules/did-you-know/' for inclusion (include_path='.:/usr/lib/php:/usr/local/lib/php') in /home/uocopstt/public_html/test2/index.php on line 107 Quote Link to comment https://forums.phpfreaks.com/topic/235283-include-random-page-from-folder-errors/#findComment-1209131 Share on other sites More sharing options...
wildteen88 Posted May 1, 2011 Share Posted May 1, 2011 Change $num = count($array); $random = rand(0, $num); $file = $array[$random]; include "modules/did-you-know/$file"; to $file = $array[ array_rand($array) ]; $chosen_file = "modules/did-you-know/$file"; if(is_file($chosen_file)) include $chosen_file; else echo "$chosen_file does not exits"; Quote Link to comment https://forums.phpfreaks.com/topic/235283-include-random-page-from-folder-errors/#findComment-1209150 Share on other sites More sharing options...
RudeYute Posted May 1, 2011 Author Share Posted May 1, 2011 Well, that shows how much I still have to learn! Seems amuch easier way of doing it, and it works perfectly! Thank you very much - hopefully that is all the PHP I will require, but I am kind of warming to it now, so who knows what this site may have! Quote Link to comment https://forums.phpfreaks.com/topic/235283-include-random-page-from-folder-errors/#findComment-1209202 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.