/    Sign up×
Community /Pin to ProfileBookmark

Is there a way in PHP to list the contents of a directory and have a link for each file automatically?

Thanks

to post a comment
PHP

4 Comments(s)

Copy linkTweet thisAlerts:
@NogDogFeb 17.2006 — [code=php]
<?php
$files = glob("{$_SERVER['DOCUMENT_ROOT']}/test/*.*");
if(count($files) > 0)
{
echo "<h3>Files:</h3>n<ul>n";
foreach($files as $path)
{
$file = basename($path);
echo "<li><a href='http://localhost/test/$file'>$file</a></li>n";
}
echo "</ul>n";
}
?>
[/code]
Copy linkTweet thisAlerts:
@CharlesFeb 17.2006 — This noob wants to know, Is [font=monospace]if(count($files) > 0)[/font] really necessary? Can it be better written as [font=monospace]if(count($files))[/font]?
Copy linkTweet thisAlerts:
@NogDogFeb 17.2006 — This noob wants to know, Is [font=monospace]if(count($files) > 0)[/font] really necessary? Can it be better written as [font=monospace]if(count($files))[/font]?[/QUOTE]
Function-wise either would work. Peformance-wise, you might save a micro-second or two without the > comparison (I'm just speculating, I have no empirical evidence). As I was thinking "If we got 1 or more files, then we're OK," and not thinking boolean, my fingers typed out a comparison instead of a true/false test. So basically I could live with either idiom, and don't consider either to necessarily be "better."
Copy linkTweet thisAlerts:
@bokehFeb 18.2006 — This noob wants to know, Is [font=monospace]if(count($files) > 0)[/font] really necessary? Can it be better written as [font=monospace]if(count($files))[/font]?[/QUOTE]It's just good practice to check the output of functions. Some functions return [I]0[/I] on success and others return values other than [I]0[/I] or [I]false[/I] on failure.
×

Success!

Help @comptech520 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 6.16,
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: @nearjob,
tipped: article
amount: 1000 SATS,

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

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...