# Php



## dudeking (Feb 7, 2007)

Hi,
I need to get this working for friday because it's my coursework.
I need the script to include all the files in a directory in numerical order.

http://beta.dudeking.co.uk/comp/news/

At the moment I am using this Script:


```
<?php
	$path = 'stories/';
	$handle = opendir($path);
		while (false!== ($file = readdir($handle))) {
			include($path.$file);
	}
	closedir($handle); 
?>
```
But have tried something like:


```
<?php
	$dh = opendir("/comp/news/stories/");
		$count='0';
			while ($entry = readdir($dh)) {
				if ((strpos($entry,'.php') !== false) && ($entry!=='index.php')) {
					if ($count < '0' ) {
						include('stories/'.$entry);
					}
				$count++;
			}
		}
	closedir($dh);
?>
```
Any help would be great


----------



## Gibble (Oct 10, 2001)

well it's failing on the path "stories/" not being valid from the directory it's running in

try "./stories"


----------



## Gibble (Oct 10, 2001)

You can also look into the realpath function.


----------



## dudeking (Feb 7, 2007)

try "./stories" made it worse.

I'll look into that now, thanks.


----------



## brendandonhu (Jul 8, 2002)

Are you trying to include all the files in a directory?

```
<?php
foreach(glob('./stories/*') as $story)
{
     include($story);
}?>
```


----------



## dudeking (Feb 7, 2007)

Yes I want all of the files.
I tried that, got this message:
Warning: main(./stories/./stories/994story.php): failed to open stream: No such file or directory in /home/fhlinux210/b/beta.dudeking.co.uk/user/htdocs/comp/include/news/scriptmain.php on line 4

Do I need the dot?


----------



## brendandonhu (Jul 8, 2002)

I edited my post a couple minutes after writing it, it looks like you might have copied it before I fixed it?

The dot means stories is in the same directory as your current script. Otherwise you need to specify the path to where it is.


----------



## dudeking (Feb 7, 2007)

Yup, that script is different 
I'll test now


----------



## dudeking (Feb 7, 2007)

OMG!!!!!!!!!!!!!!!

Thank you so much!!!!!!!!!!!!!!!!!!!!!!!!

You have no idea how much you have just helped me


----------



## dudeking (Feb 7, 2007)

Do you have any idea about why it orders them 1,2,3,6,5,4?

They should be 123456

Thanks You


----------



## brendandonhu (Jul 8, 2002)

Are you trying to sort them by filename?
You can use GLOB_NOSORT to list them as they appear in the directory. Otherwise you can use asort() to put them in order.


----------



## dudeking (Feb 7, 2007)

the files are just called 1.php 2.php 3.php ect.

I need them listing in order starting with 1.php.


----------



## brendandonhu (Jul 8, 2002)

```
<?php
$stories = glob('./stories/*');
natsort($stories);
foreach($stories as $story)
{
     include($story);
}?>
```


----------



## dudeking (Feb 7, 2007)

Still getting the dodgy ordering


----------



## brendandonhu (Jul 8, 2002)

Something else is going on then, above code should sort by number. You can use print_r() to find out where the error is.


----------



## dudeking (Feb 7, 2007)

I have had problems like this before when using scripts that list files in order.
I thought It might be to do with LB types but I have just set them to linux in dreamweaver.

I have used print_r() and it dosnt seem to be able to count lol.
The files are listed, 1,2,3,6,5,4

Could be in a problem with the server?


----------



## brendandonhu (Jul 8, 2002)

That's odd, you can try adding GLOB_NOSORT as the 2nd parameter of glob()


----------



## dudeking (Feb 7, 2007)

I don't understand what

```
foreach($stories as $story)
```
 is doing.

If I put them into an array how would I get the array to pick up new files?


----------



## brendandonhu (Jul 8, 2002)

It's all in the manual
http://us3.php.net/foreach


----------



## lizbee1 (Feb 8, 2007)

In the old days php would have been a reserved word??


----------



## dudeking (Feb 7, 2007)

I coudnt work it out.
I used an array instead


```
<?php
$stories = glob('./stories/*');
$array1 = array("1.php","2.php","3.php","4.php","5.php","6.php");
	arsort($array1);
	print_r($array1);
?>
```
How can get the array to automaticly pick-up new files in the directory?


----------



## brendandonhu (Jul 8, 2002)

That isn't listing the files inside the folder then, your glob() isn't being used.


----------



## dudeking (Feb 7, 2007)

```
<?php
$files = array("1.php","2.php","3.php","4.php","5.php","6.php");
	arsort($files);
	$number = count($files)-1;
	unset($files[$number]);
		while (list($key, $value) = each($files)){
			include('stories/'.$value);
}
?>
```
That's my code.

I want to use something like this to create the array but I am unsure about how to
<?php
foreach
(glob('stories/*') as $story){
$story array();
}
?>
[/php]


----------



## brendandonhu (Jul 8, 2002)

This isn't the same as what you were doing earlier. Why unset() the last element, and why arsort() instead of natsort()?


----------



## dudeking (Feb 7, 2007)

arsort() to list them from 6-1 rather than 1-6 and unset removes the top file in the array.
This script displays all but the most recent, the other just displays the most recent.


----------



## brendandonhu (Jul 8, 2002)

You can just replace the array() part with your glob(), and then take the 'stories/' part out of your include().

If the names of your files go into the double-digits, arsort() is going to put them in the wrong order. It would list 9.php before 10.php, for example.


----------

