dirname


SUBMITTED BY: Guest

DATE: May 27, 2014, 5:20 p.m.

FORMAT: PHP

SIZE: 2.0 kB

HITS: 16058

  1. (PHP 4, PHP 5)
  2. dirname — Returns parent directory's path
  3. Description ¶
  4. string dirname ( string $path )
  5. Given a string containing the path of a file or directory, this function will return the parent directory's path.
  6. Parameters ¶
  7. path
  8. A path.
  9. On Windows, both slash (/) and backslash (\) are used as directory separator character. In other environments, it is the forward slash (/).
  10. Return Values ¶
  11. Returns the path of the parent directory. If there are no slashes in path, a dot ('.') is returned, indicating the current directory. Otherwise, the returned string is path with any trailing /component removed.
  12. Changelog ¶
  13. Version Description
  14. 5.0.0 dirname() is now binary safe
  15. 4.0.3 dirname() was fixed to be POSIX-compliant.
  16. Examples ¶
  17. Example #1 dirname() example
  18. <?php
  19. echo "1) " . dirname("/etc/passwd") . PHP_EOL; // 1) /etc
  20. echo "2) " . dirname("/etc/") . PHP_EOL; // 2) / (or \ on Windows)
  21. echo "3) " . dirname("."); // 3) .
  22. ?>
  23. Notes ¶
  24. Note:
  25. dirname() operates naively on the input string, and is not aware of the actual filesystem, or path components such as "..".
  26. Note:
  27. dirname() is locale aware, so for it to see the correct directory name with multibyte character paths, the matching locale must be set using the setlocale() function.
  28. Note:
  29. Since PHP 4.3.0, you will often get a slash or a dot back from dirname() in situations where the older functionality would have given you the empty string.
  30. Check the following change example:
  31. <?php
  32. //before PHP 4.3.0
  33. dirname('c:/'); // returned '.'
  34. //after PHP 4.3.0
  35. dirname('c:/x'); // returns 'c:\'
  36. dirname('c:/Temp/x'); // returns 'c:/Temp'
  37. dirname('/x'); // returns '\'
  38. ?>
  39. See Also ¶
  40. basename() - Returns trailing name component of path
  41. pathinfo() - Returns information about a file path
  42. realpath() - Returns canonicalized absolute pathname

comments powered by Disqus