Club Delphi  
    FTP   CCD     Buscar   Trucos   Trabajo   Foros

Retroceder   Foros Club Delphi > Otros entornos y lenguajes > PHP
Registrarse FAQ Miembros Calendario Guía de estilo Temas de Hoy

Respuesta
 
Herramientas Buscar en Tema Desplegado
  #1  
Antiguo 11-06-2008
Avatar de Ñuño Martínez
Ñuño Martínez Ñuño Martínez is offline
Moderador
 
Registrado: jul 2006
Ubicación: Ciudad Catedral, Españistán
Posts: 6.000
Poder: 25
Ñuño Martínez Tiene un aura espectacularÑuño Martínez Tiene un aura espectacular
Rutas de búsqueda de "include" y "require".

Realmente estoy hecho un lío con las instrucciones "include", "require" y derivadas. No porque no sepa cómo funcionan, sino porque no consigo saber cómo buscan los archivos a incluir. Me da la sensación de que cada vez busca los archivos en un sitio diferente o algo. He buscado en el manual de PHP (encontré esto), pero sigo sin enterarme.

La verdad es que mi problema podría solucionarse poniendo rutas absolutas, desde la raíz, pero si (por ejemplo) a Emilio le da por cambiar el servidor pues podría dejar de funcionar.

¿Alguien podría explicarme cómo incluir archivos con rutas "relativas"? Por ejemplo:
Código PHP:
<?php
# Incluye el archivo "archivo.php" que se encuentra en el directorio padre a este archivo.
  
include_once ('../archivo.php');
?>
Esta sentencia a veces funciona como se espera y otras no (me dice que no lo encuentra, aunque exista) y no sé por qué (mismo servidor y configuración siempre).
__________________
Proyectos actuales --> Allegro 5 Pascal ¡y Delphi!|MinGRo Game Engine
Responder Con Cita
  #2  
Antiguo 11-06-2008
Avatar de dec
dec dec is offline
Moderador
 
Registrado: dic 2004
Ubicación: Alcobendas, Madrid, España
Posts: 13.107
Poder: 34
dec Tiene un aura espectaculardec Tiene un aura espectacular
Hola,

Puedes utilizar (espero) la directiva de configuración "include_path" (con la función ini_set()). Sin embargo, personalmente, suelo usar rutas absolutas. Ojo. No estoy diciendo que "include_path" no funcione: quizás es que me he acostumbrado a hacerlo como digo y nada más. Empero, el hecho de que se cambiaran los directorios en un momento dado no debería significar un problema. Me explico. Tu aplicación definiría un directorio "raíz", partiendo de la situación de un determinado "script" (que estaría en el directorio raíz) y a partir de ahí se construirían las rutas de todos los demás directorios. Entonces, si se cambia algún directorio "fuera del raíz", no pasaría nada, puesto que el directorio raíz de tu aplicación, determinado por la posición de un cierto "script", no cambiaría.

Es un poco "largo", pero, te voy a copiar abajo el archivo "gb-init.php" de Gesbit. Observa que dicho archivo se encuentra en el directorio "raíz" de Gesbit, y que, por tanto, esté donde esté situado dicho directorio, siempre será el directorio raíz para Gesbit, que es lo que importa, por decirlo así. Fíjate en el siguiente código y si tienes alguna duda pregunta.

Código PHP:
<?php

/*
   Copyrights (C) 2007-2008 David Esperalta <davidesperalta@gmail.com>

   This file is part of Gesbit (Gestor de Bitácoras in spanish)

   Gesbit is free software: you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published
   by the Free Software Foundation, either version 3 of the License,
   or (at your option) any later version.

   Gesbit is distributed in the hope that it will be useful, but WITHOUT
   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
   or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
   License for more details.

   You should have received a copy of the GNU General Public License
   along with Gesbit. If not, see <http://www.gnu.org/licenses/>.

*/

/**
 * Hi. You can see the first require script to the entrypoints of
 * Gesbit. This script define some main Gesbit constants, and then
 * require a script that initialize the Gesbit system.
 * 
 * This script is required in all Gesbit entrypoints scripts, because 
 * initialize the Gesbit system, and make available all the Gesbit 
 * global variables. Also assert the Gesbit requisites or die.
 * 
 * @package initialization
 * @subpackage entrypoints
 * @author David Esperalta <davidesperalta@gmail.com>
 * @license http://www.gnu.org/licenses/ GNU General Public License
 *
 */

/**
 * Define the root directory path. All other Gesbit related dirs
 * are relative to this directory. Note that we end the directory
 * with the appropiate separator: this is a norm in all constants.
 */
define('GB_ROOT_DIR_PATH'
  
dirname(__FILE__).DIRECTORY_SEPARATOR
);

/**
 * Define the root directory url. All other Gesbit related urls
 * are relative to this directory url. Note the diference with
 * the directory path constant: this is a url, not a system path.
 */
define('GB_ROOT_DIR_URL',str_replace(
  
'index.php','',$_SERVER['SCRIPT_NAME']
));

/**
 * Define the directory name for Gesbit admin files.
 * This is a subdirectory of {@link GB_ROOT_DIR_PATH}
 */
define('GB_ADMIN_DIR_NAME''gb-admin');

/**
 * Define the directory name for Gesbit content files.
 * This is a subdirectory of {@link GB_ROOT_DIR_PATH}
 */
define('GB_CONTENT_DIR_NAME''gb-content');

/**
 * Define the directory name for Gesbit auxiliar files.
 * This is a subdirectory of {@link GB_ROOT_DIR_PATH}
 */
define('GB_AUXILIAR_DIR_NAME''gb-auxiliar');

/**
 * Define the directory name for Gesbit error scripts.
 * This is a subdirectory of the {@link GB_AUXILIAR_DIR}
 */
define('GB_ERRORS_DIR_NAME''errors');

/**
 * Define the directory name for Gesbit locale files.
 * This is a subdirectory of the {@link GB_AUXILIAR_DIR}
 */
define('GB_LOCALE_DIR_NAME''locale');

/**
 * Define the directory name for Gesbit PHP classes.
 * This is a subdirectory of the {@link GB_AUXILIAR_DIR}
 */
define('GB_CLASSES_DIR_NAME''classes');

/**
 * Define the directory name for Gesbit requires scripts.
 * This is a subdirectory of the {@link GB_AUXILIAR_DIR}
 */
define('GB_REQUIRES_DIR_NAME''requires');

/**
 * Define the directory name for Gesbit CSS stylesheets.
 * This is a subdirectory of the {@link GB_AUXILIAR_DIR}
 */
define('GB_CSS_STYLES_DIR_NAME''styles');

/**
 * Define the directory name for Gesbit DB install script.
 * This is a subdirectory of the {@link GB_AUXILIAR_DIR}
 */
define('GB_DBINSTALL_DIR_NAME''dbinstall');

/**
 * Define the directory name for KSES PHP library.
 * This is a subdirectory of the {@link GB_CLASSES_DIR}
 */
define('GB_KSES_DIR_NAME''kses');

/**
 * Define the directory name for PHPass PHP library.
 * This is a subdirectory of the {@link GB_CLASSES_DIR}
 */
define('GB_PHPASS_DIR_NAME''phpass');

/**
 * Define the directory name for PHP Snoopy library.
 * This is a subdirectory of the {@link GB_CLASSES_DIR}
 */
define('GB_SNOOPY_DIR_NAME''snoopy');

/**
 * Define the directory name for Incutio XML-RPC library.
 * This is a subdirectory of the {@link GB_CLASSES_DIR}
 */
define('GB_IXMLRPC_DIR_NAME''ixmlrpc');

/**
 * Define the directory name for PHP GetText library.
 * This is a subdirectory of the {@link GB_CLASSES_DIR}
 */
define('GB_PHPGETTEXT_DIR_NAME''phpgettext');

/**
 * Define the directory name for Gesbit themes.
 * This is a subdirectory of the {@link GB_CONTENT_DIR}
 */
define('GB_THEMES_DIR_NAME''themes');

/**
 * Define the directory name for Gesbit default theme.
 * This is a subdirectory of the {@link GB_THEMES_DIR}
 */
define('GB_DEFAULT_THEME_DIR_NAME''default');

/**
 * Define the directory name for Gesbit plugins.
 * This is a subdirectory of the {@link GB_CONTENT_DIR}
 */
define('GB_PLUGINS_DIR_NAME''plugins');

/**
 * Define the directory path for Gesbit admin files.
 * This is a subdirectory of {@link GB_ROOT_DIR_PATH}
 */
define('GB_ADMIN_DIR',
  
GB_ROOT_DIR_PATH GB_ADMIN_DIR_NAME DIRECTORY_SEPARATOR
);

/**
 * Define the directory path for Gesbit content files.
 * This is a subdirectory of {@link GB_ROOT_DIR_PATH}
 */
define('GB_CONTENT_DIR',
  
GB_ROOT_DIR_PATH GB_CONTENT_DIR_NAME DIRECTORY_SEPARATOR
);

/**
 * Define the directory path for Gesbit auxiliar files.
 * This is a subdirectory of {@link GB_ROOT_DIR_PATH}
 */
define('GB_AUXILIAR_DIR',
  
GB_ROOT_DIR_PATH GB_AUXILIAR_DIR_NAME DIRECTORY_SEPARATOR
);

/**
 * Define the directory path for Gesbit themes.
 * This is a subdirectory of {@link GB_CONTENT_DIR}
 */
define('GB_THEMES_DIR',
  
GB_CONTENT_DIR GB_THEMES_DIR_NAME DIRECTORY_SEPARATOR
);

/**
 * Define the directory path for Gesbit plugins.
 * This is a subdirectory of {@link GB_CONTENT_DIR}
 */
define('GB_PLUGINS_DIR',
  
GB_CONTENT_DIR GB_PLUGINS_DIR_NAME DIRECTORY_SEPARATOR
);

/**
 * Define the directory path for Gesbit locale files.
 * This is a subdirectory of {@link GB_AUXILIAR_DIR}
 */
define('GB_LOCALE_DIR',
  
GB_AUXILIAR_DIR GB_LOCALE_DIR_NAME DIRECTORY_SEPARATOR
);

/**
 * Define the directory path for Gesbit require files.
 * This is a subdirectory of {@link GB_AUXILIAR_DIR}
 */
define('GB_REQUIRES_DIR',
  
GB_AUXILIAR_DIR GB_REQUIRES_DIR_NAME DIRECTORY_SEPARATOR
);

/**
 * Define the directory path for Gesbit CSS stylesheets.
 * This is a subdirectory of the {@link GB_AUXILIAR_DIR}
 */
define('GB_CSS_STYLES_DIR',
  
GB_AUXILIAR_DIR GB_CSS_STYLES_DIR_NAME DIRECTORY_SEPARATOR
);

/**
 * Define the directory path for Gesbit error scripts.
 * This is a subdirectory of the {@link GB_AUXILIAR_DIR}
 */
define('GB_ERRORS_DIR',
  
GB_AUXILIAR_DIR GB_ERRORS_DIR_NAME DIRECTORY_SEPARATOR
);

/**
 * Define the directory path for Gesbit default theme.
 * This is a subdirectory of the {@link GB_THEMES_DIR}
 */
define('GB_DEFAULT_THEME_DIR',
  
GB_THEMES_DIR GB_DEFAULT_THEME_DIR_NAME DIRECTORY_SEPARATOR
);

/**
 * Define the directory path for Gesbit PHP classes.
 * This is a subdirectory of the {@link GB_AUXILIAR_DIR}
 */
define('GB_CLASSES_DIR',
  
GB_AUXILIAR_DIR GB_CLASSES_DIR_NAME DIRECTORY_SEPARATOR
);

/**
 * Define the directory path for KSES PHP library.
 * This is a subdirectory of the {@link GB_CLASSES_DIR}
 */
define('GB_KSES_DIR',
  
GB_CLASSES_DIR GB_KSES_DIR_NAME DIRECTORY_SEPARATOR
);

/**
 * Define the directory path for PHPass library.
 * This is a subdirectory of the {@link GB_CLASSES_DIR}
 */
define('GB_PHPASS_DIR',
  
GB_CLASSES_DIR GB_PHPASS_DIR_NAME DIRECTORY_SEPARATOR
);

/**
 * Define the directory path for PHP GetText library.
 * This is a subdirectory of the {@link GB_CLASSES_DIR}
 */
define('GB_PHPGETTEXT_DIR',
  
GB_CLASSES_DIR GB_PHPGETTEXT_DIR_NAME DIRECTORY_SEPARATOR
);

/**
 * Define the directory path for Snoopy library.
 * This is a subdirectory of the {@link GB_CLASSES_DIR}
 */
define('GB_SNOOPY_DIR',
  
GB_CLASSES_DIR GB_SNOOPY_DIR_NAME DIRECTORY_SEPARATOR
);

/**
 * Define the directory path for Incutio XML-RPC library.
 * This is a subdirectory of the {@link GB_CLASSES_DIR}
 */
define('GB_IXMLRPC_DIR',
  
GB_CLASSES_DIR GB_IXMLRPC_DIR_NAME DIRECTORY_SEPARATOR
);

/**
 * Define the directory path for Gesbit DB install script.
 * This is a subdirectory of the {@link GB_AUXILIAR_DIR}
 */
define('GB_DBINSTALL_DIR',
  
GB_AUXILIAR_DIR GB_DBINSTALL_DIR_NAME DIRECTORY_SEPARATOR
);

/**
 * Define a hash to use in the Gesbit related cookie names.
 */
define('GB_COOKIE_HASH''gb-' substr(md5(GB_ROOT_DIR_PATH), 55));

/**
 * Require to initialize the Gesbit system.
 */
require(GB_REQUIRES_DIR.'gb-mainrequires.php');

?>
__________________
David Esperalta
www.decsoftutils.com

Última edición por dec fecha: 11-06-2008 a las 14:43:03.
Responder Con Cita
  #3  
Antiguo 11-06-2008
Avatar de Ñuño Martínez
Ñuño Martínez Ñuño Martínez is offline
Moderador
 
Registrado: jul 2006
Ubicación: Ciudad Catedral, Españistán
Posts: 6.000
Poder: 25
Ñuño Martínez Tiene un aura espectacularÑuño Martínez Tiene un aura espectacular
Gracias por la sugerencia, David. No se me había ocurrido echar un vistazo a lo que hay hecho por ahí porque creía que iba a ser más complicado. Olvidé por completo la existencia de __FILE__ en PHP.
__________________
Proyectos actuales --> Allegro 5 Pascal ¡y Delphi!|MinGRo Game Engine
Responder Con Cita
Respuesta



Normas de Publicación
no Puedes crear nuevos temas
no Puedes responder a temas
no Puedes adjuntar archivos
no Puedes editar tus mensajes

El código vB está habilitado
Las caritas están habilitado
Código [IMG] está habilitado
Código HTML está deshabilitado
Saltar a Foro

Temas Similares
Tema Autor Foro Respuestas Último mensaje
Comentario "aplastante" contra los que atacan la "piratería" Casimiro Notevi La Taberna 12 07-03-2010 14:03:02
Necesito llamar a métodos de clases "hija" desde su clase "padre" Flecha OOP 17 20-04-2007 00:03:53
¿cuál es mejor: "close" o "application.terminate"? unreal4u Varios 5 05-03-2007 11:01:19
"ChequeaEsto" elegido el futuro "Killer CLubDelphi" mamcx Noticias 51 31-10-2006 20:56:32
porque no me reconoce los caracteres "*" ni "%" cuando filtro mrmago Conexión con bases de datos 10 27-01-2006 04:21:16


La franja horaria es GMT +2. Ahora son las 13:02:21.


Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Traducción al castellano por el equipo de moderadores del Club Delphi
Copyright 1996-2007 Club Delphi