Trying to use DirectoryIterator to link to filies

I have recently learned about DirectoryIntegrator (thanks to this forum) and have read the PHP manual. The code I found as a starter works to list the files.

<?php
foreach (new DirectoryIterator('../myfolder') as $fileInfo) {
    if($fileInfo->isDot()) continue;
    echo $fileInfo->getFilename() . "<br>\
";
}
?>

I then tried to get the list of files and provide a link to the files. That is where my lack of knowledge showed up and I have a RuntimeException with my code.

<?php
foreach (new DirectoryIterator('../myfolder') as $fileInfo) {
    if($fileInfo->isDot()) continue;
    echo '<a href="/myfolder/' . $fileInfo->getFilename() .  ' ">' . $fileInfo->getFilename() .  '</a><br> ';
}
?>

What am I doing wrong with the echo statement?

I can see no errors in that echo statement. What exactly does the error say?

The exception will have a message and a stack trace associated with it, with those we can help further.

P.S. If you’re using PHP 5.3 (you should be) then the FilesystemIterator should be used, which would negate the need for an isDot() check.

Salathe,

I will have to ask my host to upgrade me to PHP 5.3. I currently have PHP 5.2.17.

The error is:

Fatal error: Uncaught exception ‘RuntimeException’ with message ‘DirectoryIterator::__construct(…/diocese) [<a href=‘directoryiterator.–construct’>directoryiterator.–construct</a>]: failed to open dir: No such file or directory’ in /diocese/clergy/directory_list.html:15 Stack trace: #0 /diocese/clergy/directory_list.html(15): DirectoryIterator->__construct(‘…/diocese’) #1 {main} thrown in /diocese/clergy/directory_list.html on line 15

If I change the echo statement to the un-modified echo, it will produce a list of files.

I found my problem, and it was a simple spelling error on the folder holding the files.