Page 1 of 1

Sitemap Help!

Posted: Mon Sep 29, 2003 1:16 am
by Mr. Tech
Hi!

I'm writing a script that generates a site map.

What I to be enabled to do is stop certain files from showing. The files that I don't want to be shown is in the disfiles.txt file in this format(One file per line):

Code: Select all

secret.php
hide.html
dont_show.php
And then I use PHP to select all the files and open disfiles.txt:

Code: Select all

<?php
$filecontents = file("disfiles.txt");
$handle=opendir('.');
while ($file = readdir($handle)) {
	if (is_file("./$file")) {
		for ($i=0;$i<sizeof($filecontents);$i++) {
			if ($file == $filecontents[$i] || $file == "disfiles.txt") {
			print "";
			} else {
			print "$file\n<br>";
			}
		}
	}
}
closedir($handle); 
?>
But when I go to the page that shows the files, it says this:

Code: Select all

dont_show.php 
dont_show.php 
hide.html 
hide.html 
hide.html 
index.php 
index.php 
index.php 
secret.php 
secret.php 
secret.php
Is there a way to get the for ($i=0;$i<sizeof($filecontents);$i++) function to work in the while ($file = readdir($handle)) function?

Or is there another way?

Thanks

Posted: Mon Sep 29, 2003 1:52 am
by toms100
file(filename) creates an array
you can do

Code: Select all

$fn = count($file);
$j= 0;
while ($ij < $fn) {
if ($file[$j] == $filecontents[$i] || $file[$j] == "disfiles.txt") { 
/// not sure where the $i in $filecontents comes from..
}else {
echo $file[$j]."\n<br>";
}
}
Not sure if thats what you ment...
but you could have the non allowed files in an array which is then sorted...

Posted: Mon Sep 29, 2003 1:58 am
by volka
you might use in_array to determine wether a file is to be excluded or not

Code: Select all

<?php
$filecontents = file("disfiles.txt");
// elements returned by file() will have trailing linebreaks as they were in the file
// remove them
$filecontents = array_map('trim', $filecontents);

$handle=opendir('.');
while ($file = readdir($handle)) {
	if (is_file("./$file") && !in_array($file, $filecontents))
		echo $file, "\n<br>";
}
closedir($handle);
?>
(also note the comment)
In PHP Timing Issues jason points out that array_flip/isset is faster than in_array. This still seems to be true for current versions of php and since filenames are unique for one directory you might use

Code: Select all

<?php
$filecontents = file("disfiles.txt");
// elements returned by file() will have trailing linebreaks as they were in the file
// remove them
$filecontents = array_flip(array_map('trim', $filecontents));

$handle=opendir('.');
while ($file = readdir($handle)) {
	if (is_file("./$file") && !isset($filecontents[$file]))
		echo $file, "\n<br>";
}
closedir($handle);
?>
as well

Posted: Mon Sep 29, 2003 2:25 am
by Mr. Tech
Woohoo! Thanks volka! It worked!