Jump to content

Include Random Page from Folder - Errors


RudeYute

Recommended Posts

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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++;
			}
		}

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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";

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.