Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP : List Files of Type in Directory and Link to them

Tags:

php

I've got the code

$directory = "C:/My file path";
$phpfiles = glob($directory . "*.html");

foreach($phpfiles as $phpfiles)
{
echo $phpfiles;
}

But how would I change it so that it doesn't just list the files, but actually links to them?

like image 513
e__ Avatar asked Jan 21 '12 21:01

e__


People also ask

How do I scan a directory in PHP?

To check if a folder or a file is in use, the function is_dir() or is_file() can be used. The scandir function is an inbuilt function that returns an array of files and directories of a specific directory. It lists the files and directories present inside the path specified by the user.

How can I see all PHP files in a directory?

The scandir() function returns an array of files and directories of the specified directory.

How do I find the path of a file in PHP?

The is_dir() function in PHP used to check whether the specified file is a directory or not. The name of the file is sent as a parameter to the is_dir() function and it returns True if the file is a directory else it returns False.


1 Answers

First of all, don't use same variable names at foreach(). You can link to files, like this.

foreach($phpfiles as $phpfile)
{
echo "<a href=$phpfile>".basename($phpfile)."</a>";
}

$phpfile containing full path of file (for example : /home/eray/Desktop/test.html)

basename() is returning just file name from path . basename($phpfile)'s output is test.html . If you want to print just test (without .html extension) , you can use this : basename($phpfile, ".html") Thanks, @aSeptik.

like image 177
Eray Avatar answered Sep 27 '22 19:09

Eray