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?
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.
The scandir() function returns an array of files and directories of the specified directory.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With