472,958 Members | 1,787 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,958 software developers and data experts.

Detecting PDF files in a folder and sorting them by date

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
6 8297
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Amjad | last post by:
I want to sort files in a specified folder by their creation date and then display their names (sorted by the creation date) in a textbox for example. How would you do that? Dim dir As New...
10
by: Martin Ho | last post by:
I am running into one really big problem. I wrote a script in vb.net to make a copy of folders and subfolder to another destination: - in 'from.txt' I specify which folders to copy - in...
13
by: Randall Arnold | last post by:
I'm not happy with the order in which OpenFileDialog retrieves multiple selected files. I want them in Date order, oldest to newest, but by default they come in by filename, last to first. The...
5
by: Carol | last post by:
Hi, I am in a problem. I am sending files to a particular folder one after the another. Than obviosly there create time is different. Now suppose if there are 50 files and i want to retrive the...
1
by: Peri | last post by:
Dear All, I am developing 2 applications. The first application will keep on generate a new file in a span of 30 milliseconds with some valid data inside (This code is written in C). The...
3
by: jaeden99 | last post by:
I was wandering if nyone has a script to move files older than x days old? i've seen several to delete, but I don't want to delete. I would like to create a backup of the files first verify with...
4
by: jonathan184 | last post by:
Hi I have a perl script, basically what it is suppose to do is check a folder with files. Now the files are checked using a timestamp with the command ls -l so the timestamp in this format is...
9
by: LucasLondon | last post by:
Hi, Sorry, this is a bit of a lengthy one but I guess too much information is better than less! I have an excel worksheet that I update regulary with latest values from downloaded CSV files....
8
by: =?Utf-8?B?QnJ5YW4=?= | last post by:
Hello group. I have some code (given to me), but I don't know alot about ASP, so I was hoping someone here can help. Running on Win 2008 server. The code below will scan a folder and subfolder...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.