By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,930 Members | 633 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,930 IT Pros & Developers. It's quick & easy.

Detecting PDF files in a folder and sorting them by date

P: n/a
Hi,

I am about to write an application that will display all pdf files in a
folder and display them by file name in order of date.

Is there an easy way of doing this? I was thinking about somehow adding
filename and date created in an array, sorting by date and then
printing onto an html page with a link to that file. How would I do
this though? Is this the simplest method or is there a better way of
doin this?

Cheers

Burnsy

Jul 17 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
NC
bi******@yahoo.co.uk wrote:

I am about to write an application that will display all pdf files
in a folder and display them by file name in order of date.

Is there an easy way of doing this?


$names = array();
$dates = array();
if ($handle = opendir('/path/to/files')) {
while (false !== ($file = readdir($handle))) {
if (strlen($file) - strpos(strtoupper($file), '.PDF') == 4) {
$names[] = $file;
$dates[] = filectime($file);
}
}
closedir($handle);
}
asort($dates);
foreach ($dates as $key=>$value) {
$file = $files[$key];
$date = date('Y-m-d', $value);
echo "<p><a href='$file'>$file ($date)</a></p>";
}

Cheers,
NC

Jul 17 '05 #2

P: n/a
Burnsy,
I am about to write an application that will display all pdf files in a
folder and display them by file name in order of date.

Is there an easy way of doing this? I was thinking about somehow adding
filename and date created in an array, sorting by date and then
printing onto an html page with a link to that file. How would I do
this though? Is this the simplest method or is there a better way of
doin this?


I will not write the code for you but pretty much you have the idea right.

Read the directory, if you don't want to find mimetypes and just do it
by extension find all files that have a .pdf.

Then when you find one of those files have the array do something like:
$arr[file_date_here] = ['name']. This however is not really expandable
but will work.

Then you can sort by the index of the array and do a simple foreach as
key value.

Mike

Jul 17 '05 #3

P: n/a
Thanks for that NC. Unfortunetely I was getting a few errors that I
dont know what to do with.

I am getting an error message for each file but below is an example of
one of them:

Warning: filectime(): Stat failed for SCRS.pdf (errno=2 - No such file
or directory) in /data/httpd/VirtualHosts/www/htdocs/pmu_temp/index.php
on line 32

As can be seen, it does actually recognise the file but seems to have
trouble somehow.

I did manage to use the code you provided and write the following
script:

$pdf_arr = array();
$dir = "/data/httpd/VirtualHosts/www/htdocs/pmuweb";

if ($handle = opendir($dir)) {
while (false !== ($file = readdir($handle))) {
//check if .PDF
if (strtoupper(substr($file, (strlen($file) - 4), 999)) == ".PDF") {
echo $file . "<br>";
}
}
}

This would do what I need if only it displayed files in order of date
created. Is there any way of sorting them in date order prior to
display or some point around opendir()?

Cheers

Burnsy

Jul 17 '05 #4

P: n/a
NC
bi******@yahoo.co.uk wrote:

Thanks for that NC. Unfortunetely I was getting a few errors that I
dont know what to do with.

I am getting an error message for each file but below is an example of
one of them:

Warning: filectime(): Stat failed for SCRS.pdf (errno=2 - No such file
or directory) in /data/httpd/VirtualHosts/www/htdocs/pmu_temp/index.php
on line 32
Ah, I see... My mistake: filectime() looks for files in the current
directory, not in the target one. This should solve it:

$names = array();
$dates = array();
$dir = '/path/to/files';
if ($handle = opendir($dir)) {
while (false !== ($file = readdir($handle))) {
if (strlen($file) - strpos(strtoupper($file), '.PDF') == 4) {
$names[] = $file;
$dates[] = filectime("$dir/$file");
}
}
closedir($handle);
}
asort($dates);
foreach ($dates as $key=>$value) {
$file = $names[$key];
$date = date('Y-m-d', $value);
echo "<p><a href='$dir/$file'>$file ($date)</a></p>";
}
This would do what I need if only it displayed files in order of date
created. Is there any way of sorting them in date order prior to
display or some point around opendir()?


No. readdir() reads files in the order determined by the operating
system. Any other order requires sorting by reading application.

Cheers,
NC

Jul 17 '05 #5

P: n/a
Hi,

Thats working fine in the sense that it is outputting the file and date
etc. I have changed the filectime function to filemtime so that it uses
the date modifies instead of the date created so that when someone
updated the file it will list it higher. Unfortunetely, when I right
click on a file in the directory in windows the date that is in
properties (the date I want to use) and the date that is displayed
using the filemtime is different.

Can anybody think what reason could cause this? Below is the adapted
code I am using. Ignore the extra IF statements in the last section
that basically just add check the number of files in the dir or add
<div> tags for a new month:

$names = array();
$dates = array();
$dir = '/path/to/dir/';
if ($handle = opendir($dir)) {
while (false !== ($file = readdir($handle))) {
if (strlen($file) - strpos(strtoupper($file), '.PDF') == 4) {
$names[] = $file;
$dates[] = filemtime("$dir/$file");
}
}
closedir($handle);
}

rsort($dates);
foreach ($dates as $key=>$value) {
$file = $names[$key];
$date = date('jS', $value);
echo "<div class=\"pmu_doc\"><a href='$dir/$file'>" . $file . "</a>
(" . $date . ")</div>";
}

Cheers

Burnsy

Jul 17 '05 #6

P: n/a
NC
bi******@yahoo.co.uk wrote:

I have changed the filectime function to filemtime so that it uses
the date modifies instead of the date created so that when someone
updated the file it will list it higher. Unfortunetely, when I right
click on a file in the directory in windows the date that is in
properties (the date I want to use) and the date that is displayed
using the filemtime is different.

Can anybody think what reason could cause this?


Most likely, PHP is using cached information about the files.
See documentation for clearstatcache():

http://www.php.net/clearstatcache

Cheers,
NC

Jul 17 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.