Hi,
the following script should read the files of a directory and then output a toplist of the files by views. The number of views are stored in the file counter.stats
Furthermore, the top list should provide a link to the documents listed as top 10. The link titles are the titles of the documents in the directories.
Unfortunately, the script always links to counter.php instead of index.php, and all titles are also called counter
If someone has an idea please let me know!
Thanks!
<?php
$number = 10;
$dirs [] = array("absolute" => "home/internet/test1","relative" => "./../test1");
$dirs [] = array("absolute" => "home/internet/test2","relative" => "./../test2");
$dirs [] = array("absolute" => "home/internet/test3","relative" => "./../test3");
$dirs [] = array("absolute" => "home/internet/test4","relative" => "./../test4");
$views = array();
//Absolute path to the files
for ($s = 0; $s < Count($dirs); $s++) {
$files = getFilesArray($dirs[$s]['absolute'],"stats");
for ($i = 0; $i < Count($files); $i++) {
$fp = fopen($dirs[$s]['absolute']."/".$files[$i],"r");
$tmp = trim(fread($fp,1000));
fclose($fp);
$views [] = array("rel_dir" => $dirs[$s]['relative'],"name" => substr($files[$i],0,strrpos($files[$i],".")),"views" => $tmp);
}
}
//Sorting the array
usort ($views, "compare");
function compare($a,$b) {
if ($a['views'] == $b['views']) return 0;
return ($a['views'] > $b['views']) ? -1 : 1;
}
//Creation of top list
if (Count($views) <= $number) $max = Count($views);
else $max = $number;
echo "<table>";
echo "<tr><td colspan=\"2\"><b>Top ".$number."</b></td></tr>";
for ($i = 0; $i < $max; $i++) {
echo "<tr><td><b><a href=\"".$views[$i]['rel_dir']."/".htmlentities($views[$i]['name']).".php\">".str_replace("_"," ",$views[$i]['name'])."</b></a></td><td><b>".$views[$i]['views']."</b></td></tr>";
}
echo "</table>";
//Reading files in directory
function getFilesArray($dir,$extension) {
if ($handle = @opendir($dir)) {
while (false !== ($file = readdir($handle))) {
if ($file != "." && $file != ".." && substr($file,strlen($file)-strlen($extension),strlen($extension)) == $extension) {
$fp = fopen($dir."/".$file,"r");
$files [] = $file;
}
}
closedir($handle);
return $files;
}
}
?>