Displaying filenames as Hyperlinks on a page? – PHP

  php

Q(Question):

Hi,

I’d like to write a loop that will create a hyperlink on a page for
every single file on a directory. As an example, if my directory
contains:

test.txt
test1.txt
test2.txt,

The page should then contain 3 hyperlinks pointing to these files and
the user just have to click the one he wants to download or read.

Thanks.

A(Answer):

<?php
$directory_name = "dir"; // change this to reflect your directory name
$dh = opendir($directory_name);
while ($file = readdir($dh)) {
if (is_dir("$directory_name/$file")) {
continue;
}
print "<a href=\"$directory_name/$file\">$file</a><br>\n";
}
closedir($dh);
?>
"circuit_breaker" <ci*************@canada.com> wrote in message
news:28**************************@posting.google.c om…

Hi,

I’d like to write a loop that will create a hyperlink on a page for
every single file on a directory. As an example, if my directory
contains:

test.txt
test1.txt
test2.txt,

The page should then contain 3 hyperlinks pointing to these files and
the user just have to click the one he wants to download or read.

Thanks.

A(Answer):

On Tue, 30 Mar 2004 20:37:55 GMT, "kingofkolt" <je****@comcast.net> wrote:

while ($file = readdir($dh)) {

Until you have a file called ‘0’, of course.

while (($file = readdir($dh)) !== false) {


Andy Hassall <an**@andyh.co.uk> / Space: disk usage analysis tool
http://www.andyh.co.uk / http://www.andyhsoftware.co.uk/space

A(Answer):

you’re right, i forgot about that…

another way to redo that line is:

while (!is_bool($file = readdir($dh))) {

because readdir() returns false when there are no more files

– JP

"Andy Hassall" <an**@andyh.co.uk> wrote in message
news:cl********************************@4ax.com…

On Tue, 30 Mar 2004 20:37:55 GMT, "kingofkolt" <je****@comcast.net> wrote:

while ($file = readdir($dh)) {

Until you have a file called ‘0’, of course.

while (($file = readdir($dh)) !== false) {


Andy Hassall <an**@andyh.co.uk> / Space: disk usage analysis tool
http://www.andyh.co.uk / http://www.andyhsoftware.co.uk/space

A(Answer):

kingofkolt wrote:

you’re right, i forgot about that…

another way to redo that line is:

while (!is_bool($file = readdir($dh))) {

because readdir() returns false when there are no more files

– JP

Or, perhaps a bit more clearly,
while(($file = readdir($dh)) !== false)

A(Answer):

On Tue, 30 Mar 2004 23:44:04 -0500, "Agelmar" <if**********@comcast.net> wrote:

kingofkolt wrote:

you’re right, i forgot about that…

another way to redo that line is:

while (!is_bool($file = readdir($dh))) {

because readdir() returns false when there are no more files

– JP

Or, perhaps a bit more clearly,
while(($file = readdir($dh)) !== false)

There’s an echo in here 😉 See the post he replied to but snipped 😉


Andy Hassall <an**@andyh.co.uk> / Space: disk usage analysis tool
http://www.andyh.co.uk / http://www.andyhsoftware.co.uk/space

A(Answer):

Andy Hassall wrote:

On Tue, 30 Mar 2004 23:44:04 -0500, "Agelmar"
<if**********@comcast.net> wrote:

kingofkolt wrote:

you’re right, i forgot about that…

another way to redo that line is:

while (!is_bool($file = readdir($dh))) {

because readdir() returns false when there are no more files

– JP

Or, perhaps a bit more clearly,
while(($file = readdir($dh)) !== false)

There’s an echo in here 😉 See the post he replied to but snipped
😉

LOL I didn’t look that far down. I just saw something incredibly ugly and
couldn’t resist posting ^-^.

LEAVE A COMMENT