473,382 Members | 1,387 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 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 10872
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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.