SW-Demon
June 22nd, 2004, 04:23 PM
It's userfull to add a special page to your site, called "the map of the site". It should contain hiperlinks to all pages of this site. Your visitors will be abble to see the whole contence of your site in the one place.
The easiest way to make this page is to write an ordinary htm file. But it’s borring to add links to this page every time you update your site. Another way (that actually I’ve chossen for my site) is to write a php scritpt, that will list all files on your site and display titles of htm files and hyperlinks to them. I’m going to give ypur my script.
My script has the following features:
1. For htm files it displays the title (text between <title> and </title>) and give hyperlink.
2. For zip, rar and exe it displays filename, size and gives hyperlink.
3. Separates files into groups, how they are separated into directories
4. Put “index.htm” of each directory in the top of the list.
Here is the code:
<?php
//This function lists files in dir, specified by $path,
//displays, what is supposed to be displayed,
//and calls Work for $path’s subdirectories
//(This function is recursive)
//$depth==0 for the root directory, 1 for root’s
//subdirectories, ect.
function Work($path, $depth)
{
//We are opening this dir
if ( !($h = opendir($path)) ) return 0;
//Adding slash if it’s necessary
if (substr($path, strlen($path), 1)!="/") $path="${path}/";
//Arrays, that will contain filenames
//$fa – for files, $da – directories (folders)
//$nf and $nd – number of items
$fa=array(); $da=array();
$nf=0; $nd=0;
//LISTING FILES AND FILLING THEM INTO $da AND $fa
while (false !== ($name = readdir($h)))
{
//Extra spacebars will do nothing good.
$name=trim($name);
//readdir will return “.” and “..” among
//other directories. We don’t need them.
if (($name!=".")&&($name!=".."))
{
$fullname="$path$name";
//Is it directory?
if (is_dir($fullname))
{$da[$nd]=$fullname; $nd++;}
else
{
//And if it’s file, does it have
//the proper extension?
if(($ext=strtolower(strstr($name,".")))!=false)
if (($ext==".htm")||($ext==".html")||
($ext==".php")||($ext==".asp")||
($ext==".shtml")||($ext==".rar")||
($ext==".zip")||($ext==".exe"))
{$fa[$nf]=$fullname; $nf++;}
}
}
}
closedir($h);
//DISPLAYING INFORMATION FOR FILES:
//Putting “index.htm” to the first position.
//$j – it’s current position
//$j==1 no “index.htm”
$j=-1;
for ($i=0; $i<$nf; $i++)
{
$t=substr(strtolower(trim($fa[$i])),strlen($fa[$i])-9,9);
if ( ($t=="index.htm") || ($t=="index.php")
|| ($t=="ndex.html") ) $j=$i;
}
if ($j>0) {$t=$fa[0]; $fa[0]=$fa[$j]; $fa[$j]=$t;}
//Displaying...
for ($i=0; $i<$nf; $i++)
{
//We’ll put spaces before the text,
//depending on the $depth parametr
for ($j=0; $j<$depth; $j++) echo("   ");
//is this file arhive of hipertext?
$ext=strtolower(substr($fa[$i],strlen($fa[i])-3,3));
if (($ext=="zip")||($ext=="rar")||($ext=="exe"))
{
if (($fsz=filesize($fa[$i]))!=false)
{
$fszk=round($fsz/"1024"); //it’s size
echo("<a href=$fa[$i]> $fa[$i] </a> - $fszk K <br>\r\n");
}
}
else
{
//”index” should be bold
//We’ll set $v param (bool)
$t=substr(strtolower(trim($fa[$i])),
strlen($fa[$i])-9,9);
$v=( ($t=="index.htm") || ($t=="index.php") || ($t=="ndex.html") );
//The key feature:
//EXTRACTING TITLE FROM HTML
//We’re going to look at each simbol one-by-one
//Opening this file
while ((file_exists($fa[$i]))&&(!is_readable($fa[$i])))
{
clearstatcache();
sleep( 1 );
}
if (file_exists($fa[$i]))
{
$f=fopen($fa[$i],"r");
$title=""; $a=0;
//$a==0 in regular text
//$a==1 in tags (after '<')
//$a==2 between <title> </title>
//$title keeps title
//$text (apears later) keeps the tag’s text
//Let’s go!
while (!feof($f))
{
//Working with simbol $s
$s=fread($f,1);
$b=$a;
//$b keeps new value of $a
if ($a==0) //regular text
{
if ($s=="<")
{
//tag has started
$b=1; $text="";
}
}
if ($a==1) //tag
{
if ($s==">")
{
//tag has ended
$temp=trim( strtolower($text) );
if ($temp=="title")
//it was <title>
{$title=""; $b=2;}
else
{
if ($temp=="/title")
break; //it was </title>
else $b=0; // smth other
}
}
//$s isn’t ‘>’
//it should be added to $text
else {$text="$text$s";}
}
if ($a==2) //title
{
if ($s=="<") {$b=1; $text="";}
else $title="$title$s";
}
$a=$b;
}
fclose($f); //at last!
//if file has no title
//we’ll write it’s name
if (trim($title)=="") $title=$fa[$i];
//writing this f...ing title
if ($v) echo ("<b> <font size=\"+1\">\r\n");
echo("<a href=$fa[$i]>$title</a> <br>\r\n");
if ($v) echo ("</font> </b>\r\n");
}
}
}
if ($nf>0) echo ("<br>\r\n");
if ($depth<20) // we don’t need any endless loops
{
$d=$depth+1;
for ($i=0; $i<$nd; $i++) Work($da[$i],$d);
}
} //Work function has finished !!!
//MAIN PROCEDURE:
Work(".",0);
//THE END.
?>
Sorry, that it’s so complicated…
Possible upgrades of this script:
(it will we good to do, but I’m not so enthusiastic)
1. Sorting files by titles.
2. Special configuration files to hide some paths from listing
3. Config files with commes for zip-s.
You can see, how it works, here: http://x-and-s.km.ru/map.php But there everything is in Russian, sorry.
That’s all. Your questions?
P.S. Sorry for my poor english. I hope my teacher aren’t interested in PHP and will never read this. :-]
The easiest way to make this page is to write an ordinary htm file. But it’s borring to add links to this page every time you update your site. Another way (that actually I’ve chossen for my site) is to write a php scritpt, that will list all files on your site and display titles of htm files and hyperlinks to them. I’m going to give ypur my script.
My script has the following features:
1. For htm files it displays the title (text between <title> and </title>) and give hyperlink.
2. For zip, rar and exe it displays filename, size and gives hyperlink.
3. Separates files into groups, how they are separated into directories
4. Put “index.htm” of each directory in the top of the list.
Here is the code:
<?php
//This function lists files in dir, specified by $path,
//displays, what is supposed to be displayed,
//and calls Work for $path’s subdirectories
//(This function is recursive)
//$depth==0 for the root directory, 1 for root’s
//subdirectories, ect.
function Work($path, $depth)
{
//We are opening this dir
if ( !($h = opendir($path)) ) return 0;
//Adding slash if it’s necessary
if (substr($path, strlen($path), 1)!="/") $path="${path}/";
//Arrays, that will contain filenames
//$fa – for files, $da – directories (folders)
//$nf and $nd – number of items
$fa=array(); $da=array();
$nf=0; $nd=0;
//LISTING FILES AND FILLING THEM INTO $da AND $fa
while (false !== ($name = readdir($h)))
{
//Extra spacebars will do nothing good.
$name=trim($name);
//readdir will return “.” and “..” among
//other directories. We don’t need them.
if (($name!=".")&&($name!=".."))
{
$fullname="$path$name";
//Is it directory?
if (is_dir($fullname))
{$da[$nd]=$fullname; $nd++;}
else
{
//And if it’s file, does it have
//the proper extension?
if(($ext=strtolower(strstr($name,".")))!=false)
if (($ext==".htm")||($ext==".html")||
($ext==".php")||($ext==".asp")||
($ext==".shtml")||($ext==".rar")||
($ext==".zip")||($ext==".exe"))
{$fa[$nf]=$fullname; $nf++;}
}
}
}
closedir($h);
//DISPLAYING INFORMATION FOR FILES:
//Putting “index.htm” to the first position.
//$j – it’s current position
//$j==1 no “index.htm”
$j=-1;
for ($i=0; $i<$nf; $i++)
{
$t=substr(strtolower(trim($fa[$i])),strlen($fa[$i])-9,9);
if ( ($t=="index.htm") || ($t=="index.php")
|| ($t=="ndex.html") ) $j=$i;
}
if ($j>0) {$t=$fa[0]; $fa[0]=$fa[$j]; $fa[$j]=$t;}
//Displaying...
for ($i=0; $i<$nf; $i++)
{
//We’ll put spaces before the text,
//depending on the $depth parametr
for ($j=0; $j<$depth; $j++) echo("   ");
//is this file arhive of hipertext?
$ext=strtolower(substr($fa[$i],strlen($fa[i])-3,3));
if (($ext=="zip")||($ext=="rar")||($ext=="exe"))
{
if (($fsz=filesize($fa[$i]))!=false)
{
$fszk=round($fsz/"1024"); //it’s size
echo("<a href=$fa[$i]> $fa[$i] </a> - $fszk K <br>\r\n");
}
}
else
{
//”index” should be bold
//We’ll set $v param (bool)
$t=substr(strtolower(trim($fa[$i])),
strlen($fa[$i])-9,9);
$v=( ($t=="index.htm") || ($t=="index.php") || ($t=="ndex.html") );
//The key feature:
//EXTRACTING TITLE FROM HTML
//We’re going to look at each simbol one-by-one
//Opening this file
while ((file_exists($fa[$i]))&&(!is_readable($fa[$i])))
{
clearstatcache();
sleep( 1 );
}
if (file_exists($fa[$i]))
{
$f=fopen($fa[$i],"r");
$title=""; $a=0;
//$a==0 in regular text
//$a==1 in tags (after '<')
//$a==2 between <title> </title>
//$title keeps title
//$text (apears later) keeps the tag’s text
//Let’s go!
while (!feof($f))
{
//Working with simbol $s
$s=fread($f,1);
$b=$a;
//$b keeps new value of $a
if ($a==0) //regular text
{
if ($s=="<")
{
//tag has started
$b=1; $text="";
}
}
if ($a==1) //tag
{
if ($s==">")
{
//tag has ended
$temp=trim( strtolower($text) );
if ($temp=="title")
//it was <title>
{$title=""; $b=2;}
else
{
if ($temp=="/title")
break; //it was </title>
else $b=0; // smth other
}
}
//$s isn’t ‘>’
//it should be added to $text
else {$text="$text$s";}
}
if ($a==2) //title
{
if ($s=="<") {$b=1; $text="";}
else $title="$title$s";
}
$a=$b;
}
fclose($f); //at last!
//if file has no title
//we’ll write it’s name
if (trim($title)=="") $title=$fa[$i];
//writing this f...ing title
if ($v) echo ("<b> <font size=\"+1\">\r\n");
echo("<a href=$fa[$i]>$title</a> <br>\r\n");
if ($v) echo ("</font> </b>\r\n");
}
}
}
if ($nf>0) echo ("<br>\r\n");
if ($depth<20) // we don’t need any endless loops
{
$d=$depth+1;
for ($i=0; $i<$nd; $i++) Work($da[$i],$d);
}
} //Work function has finished !!!
//MAIN PROCEDURE:
Work(".",0);
//THE END.
?>
Sorry, that it’s so complicated…
Possible upgrades of this script:
(it will we good to do, but I’m not so enthusiastic)
1. Sorting files by titles.
2. Special configuration files to hide some paths from listing
3. Config files with commes for zip-s.
You can see, how it works, here: http://x-and-s.km.ru/map.php But there everything is in Russian, sorry.
That’s all. Your questions?
P.S. Sorry for my poor english. I hope my teacher aren’t interested in PHP and will never read this. :-]