/    Sign up×
Community /Pin to ProfileBookmark

Displaying HTML links to files in alphabetical order?

Hello

I have a script that searches a folder on my server for pdf files then displays links to them on a web page in an unordered list. My questions is… is it possible to change the order that these files are displayed? Eg. alphabeticaly? I can’t work out how these files are being displayed by default… it doesn’t appear to be by modified date or alphabeticaly?

Any help greatly appreciated!

[code=php]<?php

$dom = new DOMDOcument ();
$ul = $dom->appendChild ($dom->createElement (‘ul’));

foreach (glob (‘*.pdf’) as $pdf) {
$li = $ul->appendChild ($dom->createElement (‘li’));
$a = $li->appendChild ($dom->createElement (‘a’));
$a->appendChild ($dom->createTextNode (str_replace (‘_’, ‘ ‘, pathinfo ($pdf, PATHINFO_FILENAME))));
$a->setAttribute (‘href’, $pdf);
$a->setAttribute (‘type’, ‘application/pdf’);
}

echo $dom->saveHTML();

?> [/code]

to post a comment
PHP

7 Comments(s)

Copy linkTweet thisAlerts:
@BelrickApr 27.2012 — Hi

I have a script that allows users to download pdf files from my site.

To make it simple as possible i just dump a new file in and thats it.

How i make it work is simple.

I turn the file name into effectively an array.

orderdisplayed_Title_FileDescription.pdf

[code=php]

$x = explode("_",$filename);
$title = $x[1];
$description = $x[2];

[/code]


Order displayed is of course automatic as youve discovered. So just put A or B etc or 1 or 2 etc in the file name at start.
Copy linkTweet thisAlerts:
@NogDogApr 28.2012 — You could simply assign the result of glob() to a variable, then apply the sorting function of preference (e.g. natcasesort()?) to that array, then run your foreach on the sorted array.
Copy linkTweet thisAlerts:
@BelrickApr 28.2012 — You could simply assign the result of glob() to a variable, then apply the sorting function of preference (e.g. natcasesort()?) to that array, then run your foreach on the sorted array.[/QUOTE]

Nice
Copy linkTweet thisAlerts:
@whoauthorApr 28.2012 — Thanks for your replies folks! I'll see what I can do!
Copy linkTweet thisAlerts:
@whoauthorApr 28.2012 — OK... I'm rubbish at this and am having trouble. I've been trying this:
[code=php]<?php

$dom = new DOMDOcument ();
$ul = $dom->appendChild ($dom->createElement ('ul'));

$articles = glob('*.pdf') as $pdf);

rsort($articles);

foreach $articles) {
$li = $ul->appendChild ($dom->createElement ('li'));
$a = $li->appendChild ($dom->createElement ('a'));
$a->appendChild ($dom->createTextNode (str_replace ('_', ' ', pathinfo ($pdf, PATHINFO_FILENAME))));
$a->setAttribute ('href', $pdf);
$a->setAttribute ('type', 'application/pdf');
}

echo $dom->saveHTML();

?> [/code]


I'm getting this error: Parse error: syntax error, unexpected T_AS in.......

Any guidance would be so appreciated!
Copy linkTweet thisAlerts:
@NogDogApr 29.2012 — [code=php]
$articles = glob('*.pdf');

rsort($articles);

foreach ($articles as $pdf) {
[/code]
Copy linkTweet thisAlerts:
@whoauthorApr 29.2012 — [code=php]
$articles = glob('*.pdf');

rsort($articles);

foreach ($articles as $pdf) {
[/code]
[/QUOTE]


Aha! I just couldn't see it...

Thank you so much NogDog!!! You're a star!!!
×

Success!

Help @who spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.6,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...