Automatic list files with name without path

I managed to list (images) files with their name (and without extension); this is my working code:

<?php
	   echo "<ul>";	  
$phpfiles = glob("[^index]*.jpg");
foreach ($phpfiles as $phpfile){
    $withoutExt = preg_replace('/\\.[^.\\s]{3,4}$/', '', $phpfile);
    echo '<li><img src="'.$phpfile.'" /><br /><b>'.$withoutExt.'</b></li>'; 
}	   
   echo "</ul>";
?>

But to insert this code in another folder I have to change, adding the path (from this other folder), in this way (I add only the row changed):

$phpfiles = glob("$root/my-path/[^index]*.jpg");

But so I have in the page where I want show the list a name of the file and (before) the path (i.g. …/multimedia/img/immagini-di-paesaggi/Assisi while I would only Assisi).
How could I get rid of the path?
Thank you!

Do not any string modifications on filenames with pathes. Use the right functionality instead and get a file structure.

$rii = new RecursiveIteratorIterator(new RecursiveDirectoryIterator('path/to/folder'));
foreach ($rii as $file)
{
    if ($file->isDir())
   { 
        continue;
   }
   $files[] = $file->getPathname(); 
}

there you can get all things you want like

https://www.php.net/manual/en/class.directoryiterator.php

1 Like

thank you. But with this code I still get the extension (and non jpg files) and the full path:


<?php
 $rii = new RecursiveIteratorIterator(new RecursiveDirectoryIterator("$root/multimedia/img/immagini-di-paesaggi"));
foreach ($rii as $file)
{
    if ($file->isDir())
   { 
        continue;
   }
   $files[] = $file->getPathname();   
echo '<li><img src="'.$file.'" alt="'.$file.'" onclick="onClick(this)" /><br /><b>'.$file.'</b></li>'; 
}
?>

I will do some new attempts.

EDIT

This code is almost perfect

<?php
 $rii = new RecursiveIteratorIterator(new RecursiveDirectoryIterator("$root/my/path"));
foreach ($rii as $file)
{
    if ($file->isDir())
   { 
        continue;
   }
   $files[] = $file->getPathname(); 
   $filen = $file->getBasename('.jpg');
echo '<li><img src="'.$file.'" alt="'.$file.'" onclick="onClick(this)" /><br /><b>'.$filen.'</b></li>'; 
}
?>

Almost: because I’d like list only jpg (/image) files.

So what do you think what happens when you insert something like

If(file->getExtension() == "jpg“)

Somewhere in your code?

https://www.php.net/manual/de/directoryiterator.getextension.php

1 Like

Yes I think this is correct, but where put that code?

Using my last above code, here doesn’t work

if ($file->isDir() || (file->getExtension() == 'jpg'))

neither here:

$rii = new RecursiveIteratorIterator(new RecursiveDirectoryIterator("$root/mypath(file->getExtension() == 'jpg')"));

This code works:

if ($file->isDir() || ($file->getExtension() !== 'jpg'))

But I would have a last question :blush:: how to add some other extensions (besides jpg, like webp, or png)?

let myFileTypes = [“jpg”, “png”, ……];
If(myFileTypes.includes($file->getExtension))

1 Like

So far I didn’t manage to get your code working.
It seems that “let” is not recognized and I have a white page.
Done several attempts with myFileTypes as variable ($myFileTypes), and other changes, but nothing to do.

Sorry wrong language

$myFileTypes = [“jpg”, “png”, ….];
If(in_array($file->getExtension(), $myFileTypes))

1 Like

Thank you. You code works, but to exclude some extensions.
It can be good anyway. I have now:

$myFileTypes = ["php", "inc", "txt"];