begin process at 2012 02 10 23:59:18
  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 831

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 Zip Source avec une capture GENERATEUR D'ONGLET DE NAVIGATION PHP par pos123
FORMATER UN LIEN YOUTUBE, DAILYMOTION OU VIMEO POUR L'UTILIS... par kgb93
Source avec Zip Source avec une capture PAGINATION + FICHIER CSS par profdi
Source avec Zip Source avec une capture SYSTEME D'AUTHENTIFICATION PHP AVEC PROTÉCTION KEYLOGGER par mtrix000
Source avec Zip Source avec une capture GENERATEUR DE BOUTONS DE PARTAGES POUR RESEAUX SOCIAUX par cod57

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 2012
LMMJVSD
  12345
6789101112
13141516171819
20212223242526
272829    

Consulter la suite du CalendriCode

Photothèque

 
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,718 sec (3)

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