Gestion des chemins en PHP

March 12th, 2008

Comme j'en avais marre de devoir constament verrifier si les chemins avec lesquels je travaillais finissaient oui ou non par "/", j'ai décidé de faire une classe pour gérer ça de la manière la plus simple possible. Vous pouvez télécharger cette classe aux formats 7zip, Bzip2 ou Zip.

Voici l'exemple d'utilisation que j'ai fait :

PHP:
  1. <?php
  2. // First of all, we need to include the class
  3. require_once "class.path.php";
  4.  
  5. // Then we'll create a new path, with all options
  6. // In order, the options are : the path string, is the path pointing a
  7. // directory, is there some string to put before the path, and what is the
  8. // separator ?
  9. $path = new Path("/some/path/to/test/", true, "http://", "/");
  10.  
  11. // Obviously, I want to output an URL, just look
  12. echo "It's looking like an URL: $path\n";
  13.  
  14. // Now I create a new path with a completely different delimiter
  15. $path2 = new Path("\a\file.php", false, null, "\\");
  16.  
  17. // And I can just append it to the previous one
  18. $path->a($path2);
  19. // We could have done $path->b($path2), but it would have returned a new object
  20. // with $path2 appened instead of changing $path
  21.  
  22. // And turn this into a SMB share
  23. $path->setHead("\\\\");
  24. $path->setSeparator("\\");
  25. echo "Now it's a SMB share: $path\n";
  26.  
  27. // You can also use new basename() and dirname()
  28. // Note that dirname() returns a Path and not a string
  29. echo "The parent directory is: " . $path->dirname() . "\n";
  30. echo "The pointed file is: " . $path->basename() . "\n";
  31. echo "But without its extension it would be: " . $path->basename(".php") . "\n";
  32. ?>

Entry Filed under: Programmation, PHP, Classes

Leave a Comment

hidden

Some HTML allowed:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>

Trackback this post  |  Subscribe to the comments via RSS Feed


Calendar

September 2008
M T W T F S S
« Aug    
1234567
891011121314
15161718192021
22232425262728
2930  

Most Recent Posts