I have a folder which consists of .doc and .xls files..i want to search a particular file from a folder and display the contents using php code.how to do it?i am new to php

You can use readdir to read a directory and then while you read it, you could stop reading it when (or if) you find the file you need.

For example:

 <?php
        $dir = 'folder/yourdir/';
        $opendir = opendir($dir);

        while($entry = readdir($opendir))
        {
            if(is_dir($entry))
                continue; // We don't want to list directories.

            // If you want to search for a full file name:
            if($entry == 'the file you are searching for')
            {
                //* We've found the file!

                $file = $entry;
                break; // Break the loop.
            }

            // If you want to search for a certain extension:
            $extension = substr($entry, strrpos($entry, '.') + 1); // Substr from the position of the last dot.

            if($extension == 'the_extension_you_are_searching_for')
            {
                //* We've found a file with the given extension!

                $file = $entry;
                break; // Break the loop.
            }
        }

        closedir($opendir);

You can then display the content as follows:

<?php
if($file)
{
    // Get the file's contents.
    $file_contents = file_get_contents($dir . $file);
}
else
{
    //* The file has not been found.
}

You can check out the file_get_contents() function here if you like.

thanks for your help..
I want to put a text box where the user can search for the files.Based on what the user wants,it shouls display the file for reading..

Member Avatar for diafol

You could also use 'glob'. Check some recent threads on this forum, I remember contributing to a few just this year. Something like this maybe?

$results=glob("{*.doc,*.xls}",GLOB_BRACE);
$search = $_GET['search'];
$r = array_filter($results, function ($item) use ($search) { $fn = pathinfo($item, PATHINFO_FILENAME); return (stristr($fn, $search)); });

print_r($r);

what modification i should do in this code so that whichever file user enters in the search box is displayed and is available for reading?

what modification i should do in this code so that whichever file user enters in the search box is displayed and is available for reading?

<html>

<form name="search-form" id="search-form" method="post" action="searchphp.php" enctype="multipart/form-data" style="height: 103px"> 
<legend>
<dl class="style2"> 
<dd class="style1"> 
<input tabindex="1" accesskey="b" name="search" type="text" id="search" /> 
</dd>
</dl>
<input tabindex="2" accesskey="l" type="submit" name="cmdsearch" value="Search" /> 
</form>
<?php

$myDirectory = opendir('D:\xyz');

// get each entry
while($entryName = readdir($myDirectory)) {
    $dirArray[] = $entryName;
}

// close directory
closedir($myDirectory);

//  count elements in array
$indexCount = count($dirArray);
//Print ("$indexCount files<br>\n");

// sort 'em
sort($dirArray);

// print 'em
print("<TABLE border=1 cellpadding=5 cellspacing=0 class=whitelinks>\n");
print("<TR><TH>Filename</TH><th>\n");
// loop through the array of files and print them all
for($index=0; $index < $indexCount; $index++) {
        if (substr("$dirArray[$index]", 0, 1) != "."){ // don't list hidden files
        print("<TR><TD><a href=\"$dirArray[$index]\">$dirArray[$index]</a></td>");

    }
}
print("</TABLE>\n");
?>
</html>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.