I have many .pdf files from attachments that were emailed to me by police and court of apeal (not shared via FTP) and I have the old HDD blocked so that I had to download them in same day and creation date is same with modification date, so I was wondering if it was possible to find out the date and/or time it was originally created after they are shared in a private ftp folder and listed in php? Everything I've tried to date just gives me the date/time that I download the attachment. All the files are smaller then 10MB so that I would need just a line of code to read the /CreationDate
entry via file_get_contents()
and not a class or an API with tons of files and bytes.
<?php
$date = array(); $sourcefile = "./file.pdf";
$stringedPDF = file_get_contents($sourcefile, true);
preg_match('/(?<=CreationDate )\S(?:(?<=\().+?(?=\))|(?<=\[).+?(?=\]))./', $stringedPDF, $date);
echo $all = $date[0];
A clean way would be to use a real PDF parser (see here for an example), but if you want a quick-and-dirty solution, here you are:
$pdf = file_get_contents('test.pdf');
if(preg_match('/CreationDate\\s*\\(D:([0-9]{14})/', $pdf, $date))
echo $date[1];
For a test PDF whose creation date is 2022-09-12 08:50:17, the result is:
20220912085017