0

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.

flash
  • 1,455
  • 11
  • 61
  • 132
  • `var_dump(filemtime($file)));` Probably getting false? Maybe a warning too if you check your logs. – ficuscr Jun 26 '19 at 20:32
  • After doing `var_dump(filemtime($file)));` I am getting the following error: `stat failed for 36031P.mp4 in W:\xyz\status.php on line 681 bool(false) Warning: filemtime(): stat failed for hello.mp4 in W:\xyz\status.php on line 681 bool(false)` – flash Jun 26 '19 at 20:35
  • 1
    So looks like wrong working directory, [change it](https://www.php.net/manual/en/function.chdir.php). Or, you need to use full path to file, eg. /home/foo/file1.txt – ficuscr Jun 26 '19 at 20:36
  • Where do I need to add `/` ? – flash Jun 26 '19 at 20:37
  • I have updated the code, its pulling in the above fashion. – flash Jun 26 '19 at 20:42
  • `var_dump(filemtime($src_dir . DIRECTORY_SEPARATOR . $file))); ` – ficuscr Jun 26 '19 at 20:45
  • `int(1556737016) int(1556634072)` – flash Jun 26 '19 at 20:47
  • Bingo! Make sense? You got it from here? – ficuscr Jun 26 '19 at 20:48
  • Nope, Sorry. Is there any mistake in the code? – flash Jun 26 '19 at 20:49
  • You solved your issue... 1556737016 is unix time stamp for 04/30/2019 @ 2:21pm (UTC) - simply pass it your `date` function. If you `ls` a file that doesn't exist what do you expect? If you `ls` a file that exists you get the list of directory contents. Same thing. Pass a file that exists to `filemtime` and you are good. – ficuscr Jun 26 '19 at 20:50
  • Ok I will pass it using date function but what changes I need to do here `echo date("F d Y", filemtime("$file")); echo "\t"; echo date("H:i:s", filemtime("$file"));` ? – flash Jun 26 '19 at 20:51

1 Answers1

1

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

ficuscr
  • 6,975
  • 2
  • 32
  • 52
  • 1
    You know what, in my code I have `const DS = '/';` declared, lol – flash Jun 26 '19 at 21:13
  • I did this instead `$fullFileName = $src_dir . DS . $mp4_files[$key];` – flash Jun 26 '19 at 21:14
  • FYI: [when to use DIRECTORY_SEPARATOR in PHP code?](https://stackoverflow.com/questions/26881333/when-to-use-directory-separator-in-php-code) – ficuscr Jun 26 '19 at 21:29