So I’m working on a project for a local radio station, and most of it’s finished. But there’s this little bit at the end where they want to have all the transactions from the project inserted into a CSV file local to the server. In another post I’ll outline the whole project and share the applicable code, but for now I’m going to talk about the piece that involves listing each CSV file in the server directory that has been created as a hyperlink that can be downloaded. In PHP.
The code is:
if ($handle = opendir('.')) {
while (false !== ($file = readdir($handle))) {
if ($file != "." && $file != ".." && substr($file, 0, 3) == "NTN") {
echo "<a href="$file">$file</a>";
}
}
closedir($handle);
}
I’ll go line by line and explain what each piece means in plain english:
If (the current directory can be successfully opened and assigned to $handle) {
While (reading each file name in the directory into $file, no $file causes any errors) {
If ($file does not equal "." OR ".." AND starts with "NTN") {
make a hyperlink to the file with the file name as what you would click on
}
}
close the directory
}
Drop some comments if you’ve got some other ideas or have any questions.


