Posted By

iloveitaly on 02/28/11


Tagged


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

merritt212


PHP Join Paths


 / Published in: PHP
 

  1. // http://stackoverflow.com/questions/1091107/how-to-join-filesystem-path-strings-in-php
  2. function join_paths() {
  3. $args = func_get_args();
  4. $paths = array();
  5. foreach($args as $arg) {
  6. $paths = array_merge( $paths, (array)$arg );
  7. }
  8.  
  9. foreach($paths as &$path) {
  10. $path = trim( $path, '/' );
  11. }
  12.  
  13. // remove empty elements from the array
  14. $paths = array_filter($paths);
  15.  
  16. // make sure if the path was originally an absolute path that it is kept that way
  17. if(substr( $args[0], 0, 1 ) == '/' ) {
  18. $paths[0] = '/' . $paths[0];
  19. }
  20.  
  21. return join('/', $paths);
  22. }

Report this snippet  

You need to login to post a comment.