Buscador

Iniciado por horizon, 4 Marzo 2013, 19:55 PM

0 Miembros y 1 Visitante están viendo este tema.

horizon

Buenas tardes;D

Quiero colocar un buscador en mi site. Y tengo el siguiente código:

Código (php) [Seleccionar]
$url = $_SERVER['HTTPS'] == 'on' ? 'https://' : 'http://';
$url .= $_SERVER['SERVER_PORT'] != '80' ? $_SERVER["SERVER_NAME"] . 

":" . $_SERVER["SERVER_PORT"] . $_SERVER["REQUEST_URI"] : $_SERVER

['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
$urlPath = explode("php/", $url);
$dir = 'files/'; //define your folder to search, for default is files
$files = scandir("../" . $dir); //if the folder "files" isn't in raiz 

please change "../"
$ele = array();
$total = count((array) $files);

function file_get_contents_curl($url) {
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);

    $data = curl_exec($ch);
    curl_close($ch);

    return $data;
}

foreach ($files as $key => $value) {
    $html = file_get_contents_curl($urlPath[0] . $dir . $value);
    $doc = new DOMDocument();
    @$doc->loadHTML($html);
    $nodes = $doc->getElementsByTagName('title');

    $title = $nodes->item(0)->nodeValue;

    $metas = $doc->getElementsByTagName('meta');

    for ($i = 0; $i < $metas->length; $i++) {
        $meta = $metas->item($i);
        if ($meta->getAttribute('name') == 'description')
            $description = $meta->getAttribute('content');
        if ($meta->getAttribute('name') == 'keywords')
            $keywords = $meta->getAttribute('content');
    }
    $t = substr(strrchr($value, '.'), 1);
    if ($t) {
        $file = $urlPath[0] . $dir . $value;
        if (strtolower(substr($file, stripos($file, ".htm"))) == 

".htm" || strtolower(substr($file, stripos($file, ".html"))) == 

".html" || strtolower(substr($file, stripos($file, ".asp"))) == 

".asp" || strtolower(substr($file, stripos($file, ".php"))) == 

".php") {
            $obj = array(
                "title" => $title,
                "link" => $file,
                "description" => $keywords
            );
            array_push($ele, $obj);
        } else {
            $obj = array(
                "title" => $value,
                "link" => $file,
                "description" => "image"
            );
            array_push($ele, $obj);
        }
    }

    if ($total - 1 === $key) {
        print json_encode($ele);
    }
}



El buscador, realiza la busqueda en los archivos que se encuentran en la carpeta files.
El problema surge cuando agrego carpetas en la carpeta files ejemplo: :-(

files/Ac_Dc
files/Aerosmith
files/Black_Sabbath

Quisiera saber como modificar la ruta, para que busque en la carpeta files y en todas las carpetas que voy agregando en ella.

Desde ya muchas gracias por su tiempo y por su ayuda. Buenas tardes a tds.

kalvinman

Deberias crear una base de datos, es mejor mas eficiente y mucho mas rapido, ademas de seguro ya que cuando el codigo trabaja directamente con los archivos pueden escalarse privilegios y directorios para abusar de tu codigo.