PHP trouble getting file last modified date and filesize

1.1k Views Asked by At

I am having an issue retrieving the file information from inside of a folder.

Of course, I am using PHP. I have various folders, all with various file types. Mostly .txt and .html files.

The code below displays the folders on my web page, as well as the files, but for some reason, I cannot get the correct file date or size. The file date is of course the modified date in the folder. These files are usually copied and pasted over from another location. I plan to have a batch file automatically drop the files in the directory.

Here is the code I am using:

 <?php
   function listFolderFiles($dir)
        {
            $ffs = scandir($dir);
            echo '<ul>';
            foreach($ffs as $ff)
            {
                if($ff != '.' && $ff != '..')
                {
                    echo '<li>';
                    if(is_dir($dir.'/'.$ff))
                    {
                        echo $ff;
                        listFolderFiles($dir.'/'.$ff);
                    } 
                    else 
                    {
                        echo "<a href='$dir/$ff'>$ff</a> - Last Updated: " . 
                        date('F d, Y h:i', filectime($ff)) . " - Size " . filesize($ff);
                        echo "<br />";
                    }
                    echo '</li>';
                }
            }
            echo '</ul>';
        }
    listFolderFiles('exportReports');
 ?>

As you see in the code, I used the PHP function filectime. I have also used filemtime as well. The function filectime seems to be returning the current date, whereas filemtime returns a date of December 31, 1969.

The files are in the correct directory. If I am able to display the files on my page, and the user can download the files, then why would it not retrieve the modified date? I understand that if PHP doesn't find the file, it will return the 1969 date. But it doesn't make sense. I can get the file, so why can't I get the modified date?

I have tried putting single quotes and double quotes here:

 date('F d, Y h:i', filectime("$ff")) or date('F d, Y h:i', filectime('$ff'))

But still, I get the 1969 date.

I have tried to many other PHP functions. stat(), touch(), getlastmod(). I don't what else to use.

Same thing with the filesize function. It returns a value of 0.

I don't understand.

Is there an alternative? Am I using this wrong? If so, what can I do to fix this?

Please help.

0

There are 0 best solutions below