Which part? I'll try to explain both....
Searching
You have an input field (query) and send that via POST or GET (we'll say POST) to the php page. From there, you generate start to generate your list of directories/files inside your folder. While looping through all the entries, we'll also run a quick RegEx on the filename/directory name to see if it matches the query supplied by the user:
$search_results = array(); // Will hold all search results (duh! :) )
while (false !== ($file = readdir($dh))) {
if (!is_dir("$dirpath/$file"))
{
$fileend = substr($file, -8);
$fileend = strtolower($fileend);
if ( $fileend == ".torrent" )
{
// The search part!!
if(ereg(".*".$_POST['query'].".*\.torrent", $file))
{
// The filename matches some part of the searched term
$search_results[] = $dirpath.'/'.$file
}
}
}
}
Now, we've got all our matched search results into our array, now you just loop through them and display the links!!
Pagination
Now you've got your search results, you've also got a numerical index!! With that, you can display all that have indexes between low and high (where low and high are defined by the user & your pagination links). From here, it's just a matter of [man]count/man -ing the number of entries, dividing by the max number on your page (40) and if there are less than max number $pages = 1; otherwise $pages = [man]ceil/man. Once you get that done, it's just a matter of setting up the links to go through the URL as GET variables and either send the page number, or the starting key and go through your loop.
Some basic working code for pagination might be:
$rpp = 40; // Display XX results per page
$output = '';
$max = ceil(count($results)/$rpp);
for($i=1; $i<=$max; $i++)
{
if($current == $i)
{
$output .= '<b>'.$i.'</b>'; // Don't link current page
}
else
{
$output .= '<a href="?p='.$i.'&q='.$query.'">'.$i.'</a>';
}
}
return $output;
OVERALL
Overall, your php page may look something like:
<?php
function listDir($query='.*', $dir='./torrents')
{
$dh = opendir($dir);
$search_results = array(); // Will hold all search results (duh! :) )
while (false !== ($file = readdir($dh))) {
if (is_file($dirpath.'/'.$file)) // Check to see if it's a file
{
$fileend = strtolower(substr($file, -8));
if ( $fileend == '.torrent' )
{
// The search part!!
if(ereg(".*".$query.".*\.torrent", $file))
{
// The filename matches some part of the searched term
$search_results[] = array('path'=>$dirpath.'/'.$file, 'name'=>$file);
}
}
}
}
closedir($dh); // Don't forget to free this!!
return $search_results;
}
function paginate($results, $current=1)
{
$rpp = 40; // Display XX results per page
$output = '';
$max = ceil(count($results)/$rpp);
for($i=1; $i<=$max; $i++)
{
if($current == $i)
{
$output .= '<b>'.$i.'</b>'; // Don't link current page
}
else
{
$output .= '<a href="?p='.$i.'&q='.$query.'">'.$i.'</a>';
}
}
return $output;
}
function displayTorrents($results, $cur_page=1)
{
$start = ($cur_page-1)*40; // (1-1=0*40=0; 2-1=1*40=40; 3-1=2*40=80
$output = '';
for($i=$start; $i<($start+40); $i++)
{
$output .= '<a href="parse.php?filename='.$results[$i]['file'].'">info:</a>
<a href="'.$results[$i]['path'].'">'.$results[$i]['file'].'</a><br>';
}
echo $output;
}
// Current page is either the sent value, or 1 if no page is sent
$cur_page = (isset($_GET['p']) && !empty($_GET['p']))?$_GET['p']:1;
$query = (isset($_GET['q']) && !empty($_GET['q']))?$_GET['q']:'.*';
$results = listDir($query, './torrents'); // Get search results!!
$pagination = paginate($results, $cur_page, $query); // Get pagination links!!
echo $pagination.'<br><br>'; // Display the pagination links
displayTorrents($results); // Display the results
?>
[ REVISIONS ]
1.02 Closed the directory (Free memory)
1.01 Added properly displaying paged results, not all
1.00 Original
[ SIDENOTE ]
That was one long a$$ post.... I hope you learned something....