How can I just return the file name. $image is printing absolute path name?
<?php
$directory = Yii::getPathOfAlias('webroot').'/uploads/';
$images = glob($directory . "*.{jpg,JPG,jpeg,JPEG,png,PNG}", GLOB_BRACE);
foreach($images as $image)
echo $image
?>
All I want is the file name in the specific directory not the absolute name.
Instead of
basename
, you could chdir before you glob, so the results do not contain the path, e.g.:This is probably a little faster, but won't make any significant difference unless you have tons of files
Take a look at pathinfo
http://php.net/manual/en/function.pathinfo.php
Pretty helpful function
Use
basename()
You can also remove the extension like this:
One-liner:
Example extracting only file names and converting in new array of filenames width extension.
Use php's basename