note 90789 added to function.opendir
| From: | MadogLlwyd at osu1 dot php dot net | Date: | Sun, 10 May 2009 00:22:07 +0000 |
| Subject: | note 90789 added to function.opendir | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-154405@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
?>
----
Server IP: 117.104.162.229
Probable Submitter: 114.73.63.184 (proxied: 114.73.63.184)
----
Manual Page -- http://www.php.net/manual/en/function.opendir.php
Edit -- https://master.php.net/note/edit/90789
Del: integrated -- https://master.php.net/note/delete/90789/integrated
Del: useless -- https://master.php.net/note/delete/90789/useless
Del: bad code -- https://master.php.net/note/delete/90789/bad+code
Del: spam -- https://master.php.net/note/delete/90789/spam
Del: non-english -- https://master.php.net/note/delete/90789/non-english
Del: in docs -- https://master.php.net/note/delete/90789/in+docs
Del: other reasons-- https://master.php.net/note/delete/90789
Reject -- https://master.php.net/note/reject/90789
Search -- https://master.php.net/manage/user-notes.php