Jump to content

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
https://forums.phpfreaks.com/topic/235283-include-random-page-from-folder-errors/
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

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?

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

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

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

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!

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.