phpdatefilemtime

filemtime is returning garbage values in php


I am working on a php code as shown below:

 $src_dir = ('\\\INVEST-ST-001\test\podcast\incoming_folder'); 

 $mp4_files = preg_grep('~\.(mp4)$~', scandir($src_dir));     

 $file = $mp4_files[$key];

 print_r($file);                      // Line A

 echo date("F d Y", filemtime("$file")); echo "\t"; echo date("H:i:s", filemtime("$file"));  // Line D

Line A returns the following values:

 36031P.mp4  hello.mp4

The above 2 files are placed inside incoming_folder. Last Modified Dates for the above 2 files are:

1/05/2019 2:56 PM   

30/04/2019 10:21 AM

I have used Line D to return the last modified date on the webpage but I am getting the following garbage dates instead of the actual dates above:

December 31 1969 19:00:00

December 31 1969 19:00:00

Problem Statement:

I am wondering what changes I should make at Line D so that it returns proper Last Modified Dates for the above 2 files.


Solution

  • Apparently my comments aren't sufficient. I'll explain it better. So, you need the filename to include the path, otherwise the filemtime fails since the file doesn't exist.

    Which value makes sense to pass to that function?

    \\\INVEST-ST-001\test\podcast\incoming_folder\file1.mp3 OR file1.mp3? The "current working directory" is probably where the PHP script is executing from, which is different from the path you scanned the file names.

    Basically you can resolve this by re-appending the path to the file name.

    $src_dir = ('\\\INVEST-ST-001\test\podcast\incoming_folder'); 
    
    $mp4_files = preg_grep('~\.(mp4)$~', scandir($src_dir));     
    
    $fullFileName = $src_dir . DIRECTORY_SEPARATOR . $mp4_files[$key];
    
    echo date("F d Y", filemtime($fullFileName)); 
    echo "\t"; echo date("H:i:s", filemtime($fullFileName));  // Line D