Skip to content

Instantly share code, notes, and snippets.

@littlefuntik
Created April 3, 2019 17:39
Show Gist options
  • Save littlefuntik/014661b86be7cdcc0de3bf3146eebc7b to your computer and use it in GitHub Desktop.
Save littlefuntik/014661b86be7cdcc0de3bf3146eebc7b to your computer and use it in GitHub Desktop.
<?php
$fileinfo = array(
'dirname' => '',
'basename' => '',
'filename' => '',
'extension' => ''
);
$path = '/a/b/c/abc.jpg';
$p = strrpos( $path, '/' );
$fileinfo['dirname'] = $p !== false ? substr( $path, 0, $p ) : '';
$fileinfo['basename'] = $p !== false ? substr( $path, $p + 1 ) : $path;
$p = strrpos( $fileinfo['basename'], '.' );
if ( $p !== false ) {
$fileinfo['filename'] = substr( $fileinfo['basename'], 0, $p );
$fileinfo['extension'] = substr( $fileinfo['basename'], $p + 1 );
} else {
$fileinfo['filename'] = $fileinfo['basename'];
}
var_export( $fileinfo );
/* Output:
array (
'dirname' => '/a/b/c',
'basename' => 'abc.jpg',
'filename' => 'abc',
'extension' => 'jpg',
)
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment