Jump to content

File timestamps?


onjegolders
 Share

Recommended Posts

Hi, sorry, me again, I hope these nagging questions of mine may hopefully serve other people one day :)

I have pages which contain files, which I can then link to students and I want to be able to flash up "New!" or some such like if the file has been added or updated within say 3 days.

I've figured it out to check whether the page itself containing the file has been added or modified.

$files = $file_page->files;
foreach ($file_pages as $file_page) {
$create_date = $file_page->created;
$mod_date = $file_page->modified;
$date_trigger = strtotime("-3days");
?>
<li><a href="<?php echo $file->url; ?>"><?php if ($file->description) {echo $file->description;} else { echo $file->name;} ?></a>
<?php if ($mod_date>$date_trigger && $create_date<$date_trigger) {echo "Recently modified!";} ?>
<?php if ($create_date>$date_trigger) {echo "New!";} ?>
</li>

Was wondering if it would be able to go one level deeper as I have some file pages with multiple files.

If not, it's no big deal, but would be helpful.

Thanks again.

Link to comment
Share on other sites

Each page knows when it was added and when it was last updated, since this data is contained within pages database table. There's no such data available for file fields. Your best bet would probably be creating either a custom fieldtype or (perhaps easier method) a module that hooks to page save, checks if file fields exist and if their contents have changed and then keeps a log of newly added files in page-specific (hidden) text / textarea field. JSON format is nice for storing that kind of data.

There might be a better way or this method may contain some unexpected problems, can't really test it right now, but that's what I'd suggest.

Link to comment
Share on other sites

I think you could check the "last modified date" of the file itself with php:

foreach ($file_pages as $file_page) {
 int modified = filemtime($file_page->filename);
 echo "last modified" . date('d-m-Y', modified);
}

Not testet and assuming that ->filename returns the path + the filename itself :)

  • Like 4
Link to comment
Share on other sites

I think you could check the "last modified date" of the file itself with php:

This is correct. The files themselves keep their own timestamps, # of bytes, etc., so we don't need to track it separately in the DB.

Link to comment
Share on other sites

  • 3 months later...

I have need to sort files based on timestamp (when file was added). That would be easy enough within single page or file field. But I have to do it site wide. I need to find ten newest files from whole site.

Since there is no timestamp on db level, I cannot query for this. Also looping through all the files is not an option. Is only way to create custom module with separate table (or new fieldtype)? Any hope to get timestamp supported on Pagefile? Or am I missing something obvious?

Link to comment
Share on other sites


$num = 10; // u.tell it how.manys file

ob_start();

$path = rtrim(wire('config')->paths->files, '/');

passthru("find $path -type f -exec stat -f '%m %N %m' {} \; | sort -n | tail -$num | cut -f2- -d' '");

$data = explode("\n", trim(ob_get_contents()));

ob_end_clean();

foreach($data as $line) {

list($file, $ts) = explode(' ', $line);

echo date('Y-m-d H:i:s', $ts) . " - $file<br />";

}

  • Like 5
Link to comment
Share on other sites

WillyC's example actually works. :) Returns the 10 last updated files. But the shell commands it executes are pretty unix specific (though am guessing there is an equivalent in Windows). 

We might be able to just add a timestamp field to the file-based fieldtypes too. I kind of hate to duplicate info like that, but the reality is that disk space is cheap and making them queryable from the PW API would be nice. 

  • Like 2
Link to comment
Share on other sites

WillyC examples always work, no doubts about his magic.

Our need is actually little more difficult than that - it needs to check page access, accept only pages under certain section etc. So file timestamps would definitely be great help here.

Link to comment
Share on other sites

Antti here it is if you want to give it a try. Replace these files with those attached:

/wire/modules/Fieldtype/FieldtypeFile.module

/wire/core/Pagefile.php

Test it out on a non-production setup first, as it does make schema modifications. But once installed, it adds 'modified' and 'created' fields to all file/image fields, and these can be queried in selectors as well. They initially start out at the current date/time. If you modify a file/image description or sort, that updates the 'modified' property. The 'created' property stays the same.

Let me know how this works for you? If all is good, I'll commit it to the dev branch.

FieldtypeFile.module

Pagefile.php

  • Like 2
Link to comment
Share on other sites

@ryan: I tried this out a little (by adding support for this to the SelectorTest ;) ) and it seems to be working, well, almost perfectly. When trying to get 'modified' property, the 'created' timestamp is returned instead. They do differ in the database though.

Another thing (this could be considered a feature even): when adding a new file, both properties get their values set to the time of the upload, not the time the page is saved. This doesn't really matter, but if you add a new file and write a description for it as well before saving the page, 'modified' property will be set according to the page save leaving the properties with different values although the user might consider having done one single transaction.

  • Like 2
Link to comment
Share on other sites

@Nik: Thanks for testing testing and finding the bug. Attached is the file that fixes it. think this is close to ready to commit to the dev branch, so it'll be there soon too. 

FieldtypeFile.module

Another thing (this could be considered a feature even): when adding a new file, both properties get their values set to the time of the upload, not the time the page is saved.

It actually pulls it from the file system modification time (filemtime). But those probably end up being the same. 

This doesn't really matter, but if you add a new file and write a description for it as well before saving the page, 'modified' property will be set according to the page save leaving the properties with different values although the user might consider having done one single transaction.

I was thinking that the 'created' date would always reflect the date the file was added to the system, but the 'modified' date would reflect whenever a change was made to the description or tags. So I think this disconnect is probably okay, unless you can think of a situation where it would matter. I figured this makes sense, given that ProcessWire doesn't actually let you modify a file itself… so the 'modified' is really more about the meta data. 

Link to comment
Share on other sites

  • 11 months later...

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...