Showing one icon to directory and another to other files

Asked

Viewed 352 times

2

I need to read a directory and change its icons. If it is a directory, apply an icon if it is a file. txt apply another icon.

Follow the code I made:

<?php

$dir = 'ftp/';
$pasta = opendir($dir);

while ($arquivo = readdir($pasta)){
    // Caso o arquivo tenho '.' identificaria como .txt
    if ($arquivo = '.' ){
        echo "<img src='../img/pasta.ico'>";
    } else {           
        echo "<img src='../img/archive.ico'>";
    }
}

?>
  • I forgot to add the '}' to close the while.

  • 1

    There are programming errors, one of them would be $arquivo = ta missing one more equal, because, comparison are two or three: $arquivo == or $arquivo === (with three plus compare value compares type)

  • Didn’t work out my version?

  • 1

    It worked Jorge B. Thank you. But Junior’s answer was more satisfactory.

2 answers

3

You can check if it is directory or not:

if (is_dir($dir)){

}

3


I made a version of your code, only using the class Directoryiterator, the code below checks the type and just continue the switch to get the result you want.

foreach ( new DirectoryIterator('css') as $file ) {
if ( !$file->isDot() ) {
    if ( $file->isDir() ) {
        echo $file->getBaseName();
    } else {
        switch ( $file->getExtension() ) {
            case 'txt':
                echo '<img src="txt.png">' . $file->getBaseName();
            break;

            case 'css':
                echo '<img src="css.png">' . $file->getBaseName();
            break;
        }
    }
}
}

Any questions ask in the comments.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.