note 90789 modified in function.opendir by danbrown
| From: | danbrown@php.net | Date: | Sun, 10 May 2009 21:44:35 +0000 |
| Subject: | note 90789 modified in function.opendir by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-154430@lists.php.net to get a copy of this message | ||
A simple piece to open a directory and display any files with a given extension. Great for things
like newsletters, score sheets or the like where you just want to make it easy on the user - they
just dump in the file with the correct extension and it's done. A link is given to the file
which opens up in a new window.
<?php
$current_dir = "$DOCUMENT_ROOT"."dirname/"; //Put in second part, the
directory - without a leading slash but with a trailing slash!
$dir = opendir($current_dir); // Open the sucker
echo ("<p><h1>List of available files:</h1></p><hr><br
/>");
while ($file = readdir($dir)) // while loop
{
$parts = explode(".", $file); // pull apart the name and dissect by period
if (is_array($parts) && count($parts) > 1) { // does the dissected array have more than
one part
$extension = end($parts); // set to we can see last file extension
if ($extension == "ext" OR $extension == "EXT") // is extension ext or EXT
?
echo "<a href=\"$file\" target=\"_blank\"> $file
</a><br />"; // If so, echo it out else do nothing cos it's not what we want
}
}
echo "<hr><br />";
closedir($dir); // Close the directory after we are done
?>
--was--
// A simple piece to open a directory and display any files with a given extension. Great for things
like newsletters, score sheets or the like where you just want to make it easy on the user - they
just dump in the file with the correct extension and it's done. A link is given to the file
which opens up in a new window.
<?php
$current_dir = "$DOCUMENT_ROOT"."dirname/"; //Put in second part, the
directory - without a leading slash but with a trailing slash!
$dir = opendir($current_dir); // Open the sucker
echo ("<p><h1>List of available files:</h1></p><hr><br
/>");
while ($file = readdir($dir)) // while loop
{
$parts = explode(".", $file); // pull apart the name and dissect by period
if (is_array($parts) && count($parts) > 1) { // does the dissected array have more than
one part
$extension = end($parts); // set to we can see last file extension
if ($extension == "ext" OR $extension == "EXT") // is extension ext or EXT
?
echo "<a href=\"$file\" target=\"_blank\"> $file
</a><br />"; // If so, echo it out else do nothing cos it's not what we want
}
}
echo "<hr><br />";
closedir($dir); // Close the directory after we are done
?>
http://php.net/manual/en/function.opendir.php