Search code examples
phpfunctiondirectorylast-modified

PHP show filemtime not working


I have a function that prints all files in the folder with extra info such as icon, file type and date last modified. Everything works fine if the files are not in a sub folder. For files in sub folder it does not show last modified date. I think that there might be something wrong with the file path as check return result that it does not exist. Code:

<?php 
define('PATH', 'C:/xampp/htdocs/PHP Day 10 ND/');

function printTree($kelias, $str=''){

$resursas=opendir($kelias);
    while ($failas = readdir($resursas)){
        if ($failas=='.' || $failas=='..') continue;
        if (is_dir($kelias.$failas)){
            echo "<tr><td><img src='images/folder.png'></td>";
            echo "<td>Katalogas</td><td>";
            echo $str.$failas;
            echo "</td><td>";
            echo date ("F d Y H:i:s.", filemtime($failas));
            echo "</td></tr>";
            printTree($kelias.$failas.'/',$str.'--');
        }else{
            echo "<tr>";
            if(pathinfo($kelias.$failas, PATHINFO_EXTENSION) == 'php'){
                echo "<td><img src='images/php.png'></td>";
                echo "<td>PHP</td><td>";
                echo "<a href='failas.php?file=$kelias$failas'target='_black'>$str$failas</a>";
                echo "</td><td>";
                if (file_exists($failas)) echo date ("F d Y H:i:s.", filemtime($failas));
                echo "</td></tr>";
            }
            elseif(pathinfo($kelias.$failas, PATHINFO_EXTENSION) == 'png'){
                echo "<td><img src='images/image.png'></td>";
                echo "<td>Image</td><td>";
                echo "<a href='failas.php?file=$kelias$failas'target='_black'>$str$failas</a>";
                echo "</td><td>";
                if (file_exists($failas)) echo date ("F d Y H:i:s.", filemtime($failas));
                echo "</td></tr>";
            }else{
                echo $str.$failas;

                echo "</td><td>blabla</td></tr>";
            }
        }
    }
    closedir($resursas);
}

Solution

  • On the lines with the date you forgot to add the path.

    if (file_exists($failas)) echo date ("F d Y H:i:s.", filemtime($failas));
    

    to

    if (file_exists($kelias.$failas)) echo date ("F d Y H:i:s.", filemtime($kelias.$failas));