473,624 Members | 2,502 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 10887
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(strtoupp er($file), '.PDF') == 4) {
$names[] = $file;
$dates[] = filectime($file );
}
}
closedir($handl e);
}
asort($dates);
foreach ($dates as $key=>$value) {
$file = $files[$key];
$date = date('Y-m-d', $value);
echo "<p><a href='$file'>$f ile ($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(sub str($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(strtoupp er($file), '.PDF') == 4) {
$names[] = $file;
$dates[] = filectime("$dir/$file");
}
}
closedir($handl e);
}
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(strtoupp er($file), '.PDF') == 4) {
$names[] = $file;
$dates[] = filemtime("$dir/$file");
}
}
closedir($handl e);
}

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
1903
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 DirectoryInfo("C:\myFldr") Dim myfile As FileInfo For Each myfile In dir.GetFiles("*.*") ... Next
10
7825
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 'to.txt' I specify where to copy it - After I read content of 'to.txt' I create one more subfolder named by current date and thats where everything gets to be copied
13
6103
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 only property that seems relevant is Sort, and all that does is invert the filename order. I need Date order. I've searched for an answer but nothing has come up. Any ideas?
5
1752
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 files in the order they are sent i.e. file with first create time(First in First Out). Is it possible. I couldn't find any such property of sorting. Please send any link or line of code.
1
4943
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 second application (A Windows Service written in VB.NET) will keep on read the file from the same folder and update the database. My problem is that the second appliaction will have to read the files in the same order
3
5442
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 users if it's ok to delete. Thanks in advance. i found one that was really close but it only looks at one folder. I need it to look at files within folders and subfolders too. Dim oFSO, wshShell, FileCol, oFolder, objTextFile, shareLength,...
4
2526
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 checked. Now what the script does is it checks the time stamp and creates a year folder if it does not exist and then creates a month folder if it does not exist and puts the respective files in the month folders. If the files are created this month then it...
9
2092
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. Right now, other than a couple of basic recorded macros to clean up the source data CSV files, most of this process is manual and I'l looking to automate it. I'm aware there is quite a bit of code posted on consolidating data but I've not seen...
8
1754
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 with a date/time input and return xml structure off all files that are newer than the supplied date/time. The problem is that the returned xml has path names like C:\folder\subfolder\filename.ext I would like it to be more like...
0
8246
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8179
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8685
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8490
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7174
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5570
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4184
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2612
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1489
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.