Burak
Burak

Reputation: 710

Linking files (php)


I asked before how to get the content of a directory and I got suggested dirscan() which was exactly what I needed.
Now I want it so that the files are links redirected to the corresponding file. So if there was a file named 'book.pdf', you could click it and it would open 'book.pdf'.
Right now I did this:

<?php 
$dir = '/books';
$files = scandir($dir);

foreach($files as $file) {
    echo "$file<br/>";
}
?>

Thank you!

Upvotes: 1

Views: 136

Answers (4)

Axarydax
Axarydax

Reputation: 16603

what about using HTML anchor tag? <a href="/books/$file">$file</a>

Upvotes: 1

Nanne
Nanne

Reputation: 64399

I'd say you just make them a html link? Quick piece of code:

  echo "<a href=\"{$dir}/{$file}\">{$file}</a><br/>";

Upvotes: 1

Gabi Purcaru
Gabi Purcaru

Reputation: 31524

echo "<a href='$dir/$file'>$file</a>"

Upvotes: 1

tdammers
tdammers

Reputation: 20721

Uh, make them links?

echo "<a href=\"$file\">$file</a>"

This is not so much about PHP, more about (very basic) HTML.

Upvotes: 1

Related Questions