PDA

View Full Version : [TUT] Displaying files of a directory & search function!



H0BJ0B
04-10-2008, 12:03 AM
Hey guys,

Firstly, before I start with the code - I haven't been in these forums for quite a while and so do not know what everyone is like now however this is a script I wish to share with everyone. It may not be teaching them what each step is (because I know some of you will be saying, "This isn't a TUT!") but it is helping people with something I wanted to do for ages. The scripts are in quotes because I needed to highlight text in red - which the code tags did annoyingly.

Let's say for example you have about 1000 voice clips in a directory and you do not wish to make a document, and make links to each and every one of the files. You want to simply let people view all the files with the desired style instead of leaving it as the root directory with the "Parent Directory" link visible and the "Apache" text below. To do this, you need to add the following script to your page:


<?php
$files = array();

if ($handle = opendir('DIRECTORY')) {

while (false !== ($file = readdir($handle)))
{ if ($file != "." && $file != "..")
{ $files[count($files)] = $file;
}
}
sort($files);

foreach ($files as $file) {
echo "<a href='DIRECTORY/$file'>$file</a><br>";
}
closedir($handle);
}
?>

You only need to replace the "DIRECTORY"s with the name of your directory (assuming this file is parent to it).

Now you have done this, and got all the voice clips (the files in this example), you want people to be able to search for a file. It will take ages for them to go through the whole list, so you set up a search function!

Add the code below to the part of the page you wish to make search the directory. Replace everything in red!


<form action="FILEnameTHISformISon.php" method="get">
<b>Search For A VOICECLIP:</b><br>
<input type="text" name="search" /><br>
<input type="submit" value="Find VOICECLIP" name="submit"/>
</form>

<?php
$files = array();
$searchtext = $_REQUEST['search'];
if ($searchtext=="") { echo "ERROR MESSAGE FOR NOTHING BEING ENTERED!";
} else {

if ($handle = opendir('DIRECTORY')) {
$found = false;
while (false !== ($file = readdir($handle)))
{ if (stristr($file, $searchtext))

{ $files[count($files)] = $file;
}
}
sort($files);

foreach ($files as $file) {
echo "<a href='DIRECTORY/$file'>$file</a><br>";
$found = true;
}
if (!$found) { echo "ERROR MESSAGE FOR THERE BEING NO RESULTS ON THE SEARCH!"; }
}
closedir($handle);
}
?>

I hope this has helped some people.

Thanks,
Danny

Meti
04-10-2008, 09:17 AM
I don't know PHP, but I guess it's a good TUT :P

Jack!
04-10-2008, 10:22 AM
Good TUT +Rep ive been looking for something like this for ages!!!

Fehm
04-10-2008, 10:25 AM
Rerli nice =] Thanks for posting.

Dentafrice
04-10-2008, 06:07 PM
Cleaned it up a little bit, made it all into one page, and made the variables all at the top.



<?php
/*
* Edit the 'Configuration Variables' below.
*/
$directory = "bla"; // Please leave slashes out.
$errorMessage = "There were no results found.";

/*
* Do not modify the code below.
*/

$action = $_GET ["action"];
$files = array ();
?>
<style type="text/css">
body {
font-family: Arial;
font-size: 10pt;
}
</style>

<h3>Directory Listing For: <?php
echo $directory;
?></h3>

<?php
if($action == "search") {
$searchText = $_POST ["search"];
if($handle = opendir( 'bla' )) {
$found = false;
while( false !== ($file = readdir( $handle )) ) {
if(stristr( $file, $searchText ))

{
$files [count( $files )] = $file;
}
}
sort( $files );

foreach( $files as $file ) {
echo "<a href=\"{$directory}/{$file}\">{$file}</a><br>";
$found = true;
}
if(! $found) {
echo $errorMEssage;
}
}
closedir( $handle );

} else {

if($handle = opendir( $directory )) {

while( false !== ($file = readdir( $handle )) ) {
if($file != "." && $file != "..") {
$files [count( $files )] = $file;
}
}
sort( $files );

foreach( $files as $file ) {
echo "<a href=\"{$directory}/{$file}\">{$file}</a><br>";
}
closedir( $handle );
}
}
?>

<br />
<hr size="1" color="#969696" />
<br />

<form action="?action=search" method="post"><b>Search files:</b><br>
<input type="text" name="search" /><br />
<br />
<input type="submit" value="Find File" name="submit" /></form>

L?KE
04-10-2008, 07:43 PM
Ty for this.
Ty also to dentafrice.

+rep ;)

H0BJ0B
04-10-2008, 08:54 PM
You're welcome! ;)

Want to hide these adverts? Register an account for free!