ok so i have a script that displays all files in a folder!
here it is:

<?
           $dirpath = "./torrents";

$dh = opendir($dirpath);
    while (false !== ($file = readdir($dh))) {
                   if (!is_dir("$dirpath/$file")) {
   $fileend = substr($file, -8);
         $fileend = strtolower($fileend);

if ( $fileend == ".torrent" ) {


             // If you want you could merge this into one echo
             // The info button takes you to parse.php, which is the important part
                            echo "<A HREF=\"parse.php?filename=$file\">info:</A><A HREF=\"$dirpath/$file\">$file</A><BR>";
                                       }

}
    }
     closedir($dh);


?>

Now i have 2 questions:
1. How do i make a search engine that will search the folder and display the closest matches?
2. Pagin that will only display 40 results per page!

    Paging would be done with the use of arrays. And actually, both could be done with the use of arrays.

    Paging would be nothing more than displaying array keys 0 - 39 (40 items) and then starting at 40 - 79 and so on.

    The matching part would be done with regex and something like:

    if(ereg(".*".$user_input.".*", $file))
    {
      // add to found array
    }

    Now, you have the option of storing the array in the session, or just creating it each time and with each refresh....

      just a little confused in your explanation

        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....

          MAN ur the best! and yes it was long, damn what can i do with out u

            So I take it that it worked? Or worked semi enough so you have some sand to stand upon?

              ok so im having problems with the form... with the script u gave me i named it torrent.php and now im using a form and i guess im makingthe action go to torrent.php like this:

              <table width="100%" border="0" align="center" cellpadding="0" cellspacing="1">
              <tr>
              <form id="form1" name="form1" method="post" action="torrent.php">
              <td>
              <input name="topic" type="text" id="topic" size="140" />
              <input type="submit" name="Submit" value="Submit" />
              </td>
              </form>
              </tr>
              </table>
              

              now an i doing this right?

                not exactly....

                You'll note torrent.php takes the query in the form of a GET variable, so the method needs to be GET not POST.
                You'll also notice that the query name is "q" in torrent.php not topic. So you'd either have to change the form input name to "q" or the torrent.php file from $GET['q'] to $GET['topic'].

                Other than that, looks like you got it right!!

                  interesting... seems I left a few things out.... Try this, works for me:

                  <?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($dir.'/'.$file)) // Check to see if it's a file
                    {
                  	$fileend = strtolower(substr($file, -8));
                  
                  	if ( $fileend == '.torrent' ) 
                  	{
                  	  // The search part!!
                  	  if(eregi(".*".$query.".*\.torrent", $file))
                  	  {
                  		// The filename matches some part of the searched term
                  		$search_results[] = array('path'=>$dir.'/'.$file, 'name'=>$file);
                  	  }
                  	}
                    }
                  }
                  
                  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 = '';
                  	if(count($results)>40)
                  	{
                  		$max = ($cur_page-1)*40;
                  	}
                  	else
                  	{
                  		$max = count($results);
                  	}
                  	for($i=$start; $i<$max; $i++)
                  	{
                  		$output .= '<a href="parse.php?filename='.$results[$i]['name'].'">info:</a>
                  		<a href="'.$results[$i]['path'].'">'.$results[$i]['name'].'</a><br>';
                  	}
                  
                  echo $output;
                  }
                  
                  function displaySearch()
                  {
                  	echo '
                  <form action="'.$_SERVER['PHP_SELF'].'" method="get">
                  	Search: <input type="text" name="q"><input type="submit" name="submit" value="Search">
                  </form>';
                  }
                  
                  displaySearch();
                  
                  // 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 'Pages: '.$pagination.'<br><br>'; // Display the pagination links
                  displayTorrents($results, $cur_page); // Display the results
                  
                  ?>

                  Updated to include case-insensitive-ness

                    im sorry let me correct that, it works but only if you type it correctly, like if the name is Adobe and i put adobe itwont work cuz its not capitalized, so how do i fix it

                      do an eregi() test instead of ereg.......

                        ok, ur the best!
                        and i was trying to do a if statement for the pages like:

                        if ( $result = 0 ){
                        echo 'There are no results';
                        }else{

                        but that doesnt work so well

                        like if there is no pages then
                        echo'There are no torrents';

                          and also, i cant beleive i forgot this, but how do i display a text from url by using $_get and placing it in a echo

                            huh? LIke:

                            echo $_GET['some_var'];

                            or

                            echo urldecode($_GET['some_var']);

                            Not quite sure I understand....

                            And for having no results display, here's a hint: If nothing matches, the array will be empty.....

                              so lets say
                              if (emtpy[$pagination]){
                              echo 'bla bla';
                              }else{

                              dont know if thats right, im making educated guesses, lol, but correct my coding if im wrong

                                as for the get i wanted it to be like

                                echo' you seached for "GET URL" ';

                                  SO CLOSE!!!! 😉

                                  if(empty($results))
                                  {
                                    echo 'Sorry, no results found. :(';
                                  }
                                  else
                                  {
                                    // Now do your pagination and list display
                                  }

                                  And as for echoing the query:

                                  echo 'You searched for '.urldecode($_GET['q']);

                                    lol, i m learning...wooot! lol, man, i wanna say THANKS MAN for teaching me! ur the best!
                                    but here is my last question for this topic

                                    how do you get php variables and codes from another php page, like

                                    include or require (i guess)