elhacker.net cabecera Bienvenido(a), Visitante. Por favor Ingresar o Registrarse
¿Perdiste tu email de activación?.

 

 


Tema destacado: Rompecabezas de Bitcoin, Medio millón USD en premios


+  Foro de elhacker.net
|-+  Programación
| |-+  Desarrollo Web
| | |-+  PHP (Moderador: #!drvy)
| | | |-+  Buscador
0 Usuarios y 1 Visitante están viendo este tema.
Páginas: [1] Ir Abajo Respuesta Imprimir
Autor Tema: Buscador  (Leído 1,311 veces)
horizon

Desconectado Desconectado

Mensajes: 1


Ver Perfil
Buscador
« en: 4 Marzo 2013, 19:55 pm »

Buenas tardes;D

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

Código
  1. $url = $_SERVER['HTTPS'] == 'on' ? 'https://' : 'http://';
  2. $url .= $_SERVER['SERVER_PORT'] != '80' ? $_SERVER["SERVER_NAME"] .  
  3.  
  4. ":" . $_SERVER["SERVER_PORT"] . $_SERVER["REQUEST_URI"] : $_SERVER
  5.  
  6. ['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
  7. $urlPath = explode("php/", $url);
  8. $dir = 'files/'; //define your folder to search, for default is files
  9. $files = scandir("../" . $dir); //if the folder "files" isn't in raiz  
  10.  
  11. please change "../"
  12. $ele = array();
  13. $total = count((array) $files);
  14.  
  15. function file_get_contents_curl($url) {
  16.    $ch = curl_init();
  17.    curl_setopt($ch, CURLOPT_HEADER, 0);
  18.    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  19.    curl_setopt($ch, CURLOPT_URL, $url);
  20.    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
  21.  
  22.    $data = curl_exec($ch);
  23.    curl_close($ch);
  24.  
  25.    return $data;
  26. }
  27.  
  28. foreach ($files as $key => $value) {
  29.    $html = file_get_contents_curl($urlPath[0] . $dir . $value);
  30.    $doc = new DOMDocument();
  31.    @$doc->loadHTML($html);
  32.    $nodes = $doc->getElementsByTagName('title');
  33.  
  34.    $title = $nodes->item(0)->nodeValue;
  35.  
  36.    $metas = $doc->getElementsByTagName('meta');
  37.  
  38.    for ($i = 0; $i < $metas->length; $i++) {
  39.        $meta = $metas->item($i);
  40.        if ($meta->getAttribute('name') == 'description')
  41.            $description = $meta->getAttribute('content');
  42.        if ($meta->getAttribute('name') == 'keywords')
  43.            $keywords = $meta->getAttribute('content');
  44.    }
  45.    $t = substr(strrchr($value, '.'), 1);
  46.    if ($t) {
  47.        $file = $urlPath[0] . $dir . $value;
  48.        if (strtolower(substr($file, stripos($file, ".htm"))) ==  
  49.  
  50. ".htm" || strtolower(substr($file, stripos($file, ".html"))) ==  
  51.  
  52. ".html" || strtolower(substr($file, stripos($file, ".asp"))) ==  
  53.  
  54. ".asp" || strtolower(substr($file, stripos($file, ".php"))) ==  
  55.  
  56. ".php") {
  57.            $obj = array(
  58.                "title" => $title,
  59.                "link" => $file,
  60.                "description" => $keywords
  61.            );
  62.            array_push($ele, $obj);
  63.        } else {
  64.            $obj = array(
  65.                "title" => $value,
  66.                "link" => $file,
  67.                "description" => "image"
  68.            );
  69.            array_push($ele, $obj);
  70.        }
  71.    }
  72.  
  73.    if ($total - 1 === $key) {
  74.        print json_encode($ele);
  75.    }
  76. }


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.


En línea

kalvinman

Desconectado Desconectado

Mensajes: 25

Lorem ipsum dolor sit amet...


Ver Perfil WWW
Re: Buscador
« Respuesta #1 en: 5 Marzo 2013, 23:12 pm »

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.


En línea

Páginas: [1] Ir Arriba Respuesta Imprimir 

Ir a:  

Mensajes similares
Asunto Iniciado por Respuestas Vistas Último mensaje
Buscador
Sugerencias y dudas sobre el Foro
celeron7777777 2 2,274 Último mensaje 27 Noviembre 2004, 10:39 am
por ...:::SuPrA:::...
¿buscador? « 1 2 »
Sugerencias y dudas sobre el Foro
buhomaster 10 4,666 Último mensaje 24 Noviembre 2006, 09:56 am
por Neobius
Buscador
Scripting
Nilrac 2 2,293 Último mensaje 7 Febrero 2010, 18:31 pm
por Nilrac
WAP2 - Aviso Legal - Powered by SMF 1.1.21 | SMF © 2006-2008, Simple Machines