Question

Im using the following to list the files in a directory in a intranet site I am making. The problem is that is is also listing the path to the file too, does anyone know what im doing wrong ?. thanks :-)

    <?php 
    foreach (new RecursiveIteratorIterator(new RecursiveDirectoryIterator('./customer-files/28734f6d045f8a5a93.18936710')) as $filename) 
    { 
    echo '<p>';
          echo "$filename\n";
    echo '</p>'; 
    } 
    ?> 
Was it helpful?

Solution

You know the path you're passing, just use either:

str_replace($path,'',$filename);

or

substr($filename,strlen($path));

If you don't want ANY PATH in there, you can just get the filename with $filename->getFilename(); however, that will lead to confusion, as subdirectories won't be visible.

OTHER TIPS

Surely you can just use basename()

echo basename($filename) . "\n";
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top