basename
RETURNS the rightmost name component of the
path.
This function operates naively on the input string, and is not aware of the actual filesystem, or path components such as ( .. ).
This function is LOCALE dependent, so for it to see the correct directory name with multibyte character paths.
<?php
str basename ( str $path , str $suffix = "" )
where,
$path = The path to be searched
$suffix = The suffix of the files to be listed and cut off
?>
$path
The path to be searched.
On Windows, both slash (/) and backslash (\) are used as directory separator character. In other environments, it is the forward slash (/).
$suffix
If the name component ends in suffix this will also be cut off.
EXERCISE
<?php
$locl01 = 'Your Wildest Dreams.m4a';
echo basename($locl01 . '') . '<br><br>' . basename($locl01) .
'<br><br>' . basename($locl01, '.m4a');
?>
EXERCISE
<?php
$lk02 = __DIR__ . '/';
$tst02 = glob($lk02 . "temp/*{.JPG,.jpg,.txt,.TXT,.mp3,.MP3,.wav,.WAV,.m4a,.M4A}",
GLOB_BRACE);
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
This code retrieves all files in the
specified directory, if they are:
.JPG,.jpg,.txt,.TXT,.mp3, .MP3, .wav, .WAV, .m4a, .M4A
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
foreach($tst02 as $ts02)
{
echo basename($ts02) . '<br>';
}
?>
EXERCISE
<?php
$sufix03 = '{*.txt, *.mp3,*.MP3,*.wav,*.WAV}';
$path03 = __DIR__ . '/temp/';
$tst03 = glob($path03 . $sufix03, GLOB_BRACE);
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
This code retrieves all files in the
specified directory, if they are:
.txt, .mp3,.MP3,.wav,.WAV
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
foreach($tst03 as $ts03)
{
echo basename($ts03, '.txt') . '<br>';
}
?>
EXERCISE
<?php
$lk04 = __DIR__ . '/temp/';
$tst04 = glob($lk04, GLOB_ONLYDIR);
foreach($tst04 as $ts04)
{
$files = glob($ts04 .
"*{.jpg, .JPG}", GLOB_BRACE );
foreach ($files as $fil)
{
echo basename($fil, '.jpg') . '<br>';
}
}
?>
EXERCISE
<?php
$p05 = '../../path to/mus/*/*/';
$tst05 = glob($p05, GLOB_ONLYDIR);
foreach($tst05 as $ts05)
{
$files = glob($ts05 .
"*{.m4a,.M4A}", GLOB_BRACE );
echo '<br>';
foreach ($files as $fil)
{
echo basename($fil, '.m4a') . '<br>';
}
}
// This code provides the list,
// only, for files: m4a
// stored in the folder:
// ../../path to/mus/*/*/
// and does not includes all its prefixes
// In the '../../path to/mus/*/*/' path,
// the sequence '/*/*/' means that
// it must be checked two levels
// above '../../path to/mus/f1/f2'
/* - - - - - - - - - - - - - - - - - - - - - - - - -
The "../../path to/mus/f1/f2"
should certainly exist
- - - - - - - - - - - - - - - - - - - - - - - - - - */
?>
EXERCISE
<?php
define('PATH2FILES', 'H://../path to/';
$tst06 = glob(PATH2FILES .
"*{.PNG,.png,.txt,.TXT,.mp3,.MP3,.wav,.WAV,.m4a,.M4A}",
GLOB_BRACE);
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
PATH2FILES = The real path for the files
This code try to retrieve all files in the
specified directory, if they are:
.PNG,.png,.txt,.TXT,.mp3,
.MP3, .wav, .WAV, .m4a, .M4A
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
foreach($tst06 as $ts06)
{
echo basename($ts06) . '<br>';
}
?>