Showing one icon for directory and another icon for other files

2

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

Here is the code I did:

<?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'>";
    }
}

?>
    
asked by anonymous 14.04.2014 / 16:34

2 answers

3

I made a version of your code, but using the DirectoryIterator class, 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 please ask in the comments.

    
14.04.2014 / 16:53
3

You can check whether or not it is a directory:

if (is_dir($dir)){

}
    
14.04.2014 / 16:47