user1182132
user1182132

Reputation: 1

php filemtime returning false other then the php application's root directory

I am developing a php application using Ubuntu 11.10 in development environment. I have a directory '/usr/src/app_name/' which contains some pdf files. I need to retrieve the file names along with their last modification time.

The following is the code snippet:

foreach (new DirectoryIterator('/usr/src/app_name') as $file) {
    if($file->isDot()) continue;
      print $file->getFilename().' : '.date ("F d Y H:i:s.", filemtime($file)).'<br>';
}

I get the file names correctly but gives me: PHP Warning: filemtime(): stat failed.... and I get the time as 1 January, 1970.

But, if I change the directory to application root directory i.e.

foreach (new DirectoryIterator('/var/www/app_name') as $file) {
  if($file->isDot()) continue;
    print $file->getFilename().' : '.date ("F d Y H:i:s.", filemtime($file)).'<br>';
 }

I get all the names of the php files of the app along with their last modification time.

Any help on this will much appreciated....Thanks in advance.

Upvotes: 0

Views: 2361

Answers (1)

Quentin Pradet
Quentin Pradet

Reputation: 4771

I know I already said that in a comment, but at the time I could not know if it was the issue or not. So here is the full explanation.

Using filemtime($file->getRealPath()) could help solve the issue since a comment on the doc of filemtime mentions that you need to feed absolute paths to filemtime. Now using the result of the iterator directly (your $file variable) is probably going to call DirectoryIterator::__toString which only returns the name of the file, and not the absolute path.

It could have worked for /var/www because it was the working directory, but I can't be sure.

Upvotes: 1

Related Questions