Skip to content

Instantly share code, notes, and snippets.

@avalanche123
Created January 25, 2011 17:09
Show Gist options
  • Save avalanche123/795230 to your computer and use it in GitHub Desktop.
Save avalanche123/795230 to your computer and use it in GitHub Desktop.
<?php
// extract file name and path from a full file path /tmp/images/image.jpg
$path = '/tmp/images/image.jpg';
list($name, $dir) = array_map('strrev', explode(DIRECTORY_SEPARATOR, strrev(str_replace('/', DIRECTORY_SEPARATOR, $path)), 2));
// or , thanks to @nicholascloud
list($dir, $name) = array_values(pathinfo($path));
@nicholascloud
Copy link

@bobthecow
Copy link

I'm with @nicholascloud :)

@avalanche123
Copy link
Author

updated :)

@komapa
Copy link

komapa commented Jan 25, 2011

list($dir, $name) = pathinfo($path);

;)

@avalanche123
Copy link
Author

that wouldn't work as list only accepts numerically indexed array, and cannot work with associative one, hence the array_values(...)

@bobthecow
Copy link

relying on the value order in array_values seems a bit ugly :-/

@docteurklein
Copy link

what about

list($dir, $name) = array(pathinfo($path, PATHINFO_DIRNAME), pathinfo($path, PATHINFO_FILENAME));

didn't tested :)

@bobthecow
Copy link

how 'bout

$dir = pathinfo($path, PATHINFO_DIRNAME);
$name = pathinfo($path, PATHINFO_FILENAME);

seems pretty straightforward :)

@avalanche123
Copy link
Author

you guys win:)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment