begin process at 2010 02 10 00:59:30
  Trouver un code source :
 
dans
 
Accueil > 

Code

 > 

Astuces

 > TROUVER UN CHEMIN D'ACCES RELATIF

TROUVER UN CHEMIN D'ACCES RELATIF


 Information sur la source

Note :
Aucune note
Catégorie :Astuces Niveau :Débutant Date de création :17/02/2004 Date de mise à jour :18/02/2004 14:13:27 Vu :3 097

Auteur : wonesek

Ecrire un message privé
Site perso
Commentaire sur cette source (2)
Ajouter un commentaire et/ou une note

 Description

Cette petite fonction permet de créer un chemin d’accès relatif à partir de deux chemins d'accès absolus.

Source

  • <?
  • if (!function_exists("path_rewrite")) {
  • function path_rewrite($path_from, $path_to, $from_file, $to_file, $control = TRUE) {
  • //chemin d'acces qui sera retourné
  • $path_final = array();
  • //on change tous les "\" en "/"
  • $path_from = str_replace("\\", "/", $path_from);
  • $path_to = str_replace("\\", "/", $path_to);
  • //on supprime l'antislash final si il y a
  • $path_from = ereg_replace("/$", "", $path_from);
  • $path_to = ereg_replace("/$", "", $path_to);
  • //on ajoute le / au debut si nesessaire
  • if (!$control) {
  • $path_from = ereg_replace("^/?", "/", $path_from);
  • $path_to = ereg_replace("^/?", "/", $path_to);
  • }
  • //on split les url repertoire par repertoire
  • $path["from"] = explode("/", $path_from);
  • $path["to"] = explode("/", $path_to);
  • if ($from_file) array_pop($path["from"]);
  • $to_file = ($to_file) ? array_pop($path["to"]) : "";
  • //on regarde leur racine commune
  • for ($root = 0; $root < count($path["from"]), $root < count($path["to"]); $root++) {
  • if ($path["from"][$root] != $path["to"][$root]) break;
  • }
  • if ($control && $root == 0) return 0;
  • //on construit le chemin d'acces
  • if (count($path["from"]) == $root) {
  • $path_final[] = ".";
  • } else {
  • $path_final = array_pad($path_final, count($path["from"]) - $root, "..");
  • }
  • for ($i = $root; $i < count($path["to"]); $i++) $path_final[] = $path["to"][$i];
  • return (implode("/", $path_final) . "/" . $to_file);
  • }
  • }
  • ?>
<?
if (!function_exists("path_rewrite")) {

	function path_rewrite($path_from, $path_to, $from_file, $to_file, $control = TRUE) {

		//chemin d'acces qui sera retourné
		$path_final = array();

		//on change tous les "\" en "/"
		$path_from	= str_replace("\\", "/", $path_from);
		$path_to	= str_replace("\\", "/", $path_to);

		//on supprime l'antislash final si il y a
		$path_from	= ereg_replace("/$", "", $path_from);
		$path_to	= ereg_replace("/$", "", $path_to);

		//on ajoute le / au debut si nesessaire
		if (!$control) {
			$path_from	= ereg_replace("^/?", "/", $path_from);
			$path_to	= ereg_replace("^/?", "/", $path_to);
		}

		//on split les url repertoire par repertoire
		$path["from"]	= explode("/", $path_from);
		$path["to"]	= explode("/", $path_to);

		if ($from_file)	array_pop($path["from"]);
		$to_file = ($to_file) ? array_pop($path["to"]) : "";

		//on regarde leur racine commune
		for ($root = 0; $root < count($path["from"]), $root < count($path["to"]); $root++) {
			if ($path["from"][$root] != $path["to"][$root]) break;
		}

		if ($control && $root == 0) return 0;

		//on construit le chemin d'acces
		if (count($path["from"]) == $root) {
			$path_final[] = ".";
		} else {
			$path_final = array_pad($path_final, count($path["from"]) - $root, "..");		
		}

		for ($i = $root; $i < count($path["to"]); $i++) $path_final[] = $path["to"][$i];

		return (implode("/", $path_final) . "/" . $to_file);
	}
}
?>

 Conclusion

function path_rewrite(string from, string to, bool from_file, bool to_file, [bool control])

Arguments:
from et to sont deux chemins d'accès absolus. From est le répertoire ou le fichier d'où l'on part, to est le fichier ou répertoire ou l'on va.
From_file indique si le chemin d'accès From est celui d'un fichier (TRUE) ou celui d'un répertoire (FALSE).
To_file indique si le chemin d'accès To est celui d'un fichier (TRUE) ou celui d'un répertoire (FALSE).
Control. Vaut TRUE si la fonction doit vérifier qu'on peut bien passer de  from à to. Vaut FALSE si on sous entend qu'on peut accéder de from à to. Par défaut vaut TRUE.

Quelques exemples pour faire passer la pilule :)

path_rewrite("a/", "a/b/c/", FALSE, FALSE);
Retournera "./b/c/"

path_rewrite("a/b/c", "a/", FALSE, FALSE);
Retournera "../../"

path_rewrite("a/b", "a/d/f", FALSE, FALSE);
Retournera "../d/f/"

path_rewrite("/a/fichier1.php", "a/b/fichier2.php", TRUE, TRUE)
Retournera "0" car a/b/fichier2.php est un chemin d'accès relatif

path_rewrite("/a/fichier1.php", "a/b/fichier2.php", TRUE, TRUE, FALSE)
Retournera "./b/fichier2.php" : control est FALSE revient au même de faire :
path_rewrite("/a/fichier1.php", "/a/b/fichier2.php", TRUE, TRUE)

Quelques infos:
-le script fonctionne aussi bien avec des "/" que des "\"
-le trailing slash n'est pas utile grâce à from_file et à to_file
-le if (!function_exists("path_rewrite")) { peut être utile si vous n'utilisez pas un include_one()

Bon en fin de compte j'espere que m'aurez compris... (J'explique mal je sais) enfin bref hésitez pas a poser vos questions ! ;)

A bientôt !


 Sources du même auteur

Source avec Zip HOW LONG! V1.0
Source avec Zip COMPTEUR DE VISITE DE VOTRE SITE
Source avec Zip COMPTEUR DE VISITE D'UN INTERNAUTE
Source avec Zip COMPTEUR TEMPS REEL VERSION 2

 Sources de la même categorie

Source avec une capture PAGINATION EN PHP par Orangina
Source avec Zip POO - DEBUGGER par DiGhan
Source avec Zip CRAWLER DE SITE EN PHP par Mcjo
DÉCOUPAGE D'UN TEXTE EN FONCTION DES SAUTS DE LIGNES par biloubil
RÉCUPÉRER LE CHEMIN RELATIF D'UN OBJET PAR RAPPORT À LA RACI... par FredPsy

Commentaires et avis

Commentaire de michiki le 18/02/2004 09:26:58

j&#8217 j&#8217 j&#8217 ;-)

Commentaire de powange le 15/08/2006 15:37:39

Très bien comme script, il me sera utile pour plusieurs chose.. bien détaillé expliquer et tout..

 Ajouter un commentaire




Nos sponsors


Sondage...

Comparez les prix

CalendriCode

Février 2010
LMMJVSD
1234567
891011121314
15161718192021
22232425262728

Consulter la suite du CalendriCode

 
Développement réalisé par Nicolas SOREL (Nix) avec l'aide de : Cyril DURAND et Emmanuel (EBArtSoft), Merci à Vincent pour ses précieux conseils.
CodeS-SourceS.com© Toute reproduction même partielle est interdite sauf accord écrit du Webmaster
CodeS-SourceS.com© est une marque déposée tous droits réservés

Google Coop CodeS-SourceS Google Coop CodeS-SourceS
Temps d'éxécution de la page : 0,406 sec (3)

Nous contacter | Annoncer sur CodeS-SourceS | Mentions légales