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

Recommended Answers

All 5 Replies

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:

        $dir = 'folder/yourdir/';
        $opendir = opendir($dir);

        while($entry = readdir($opendir))
                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.


You can then display the content as follows:

    // Get the file's contents.
    $file_contents = file_get_contents($dir . $file);
    //* 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

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?

$search = $_GET['search'];
$r = array_filter($results, function ($item) use ($search) { $fn = pathinfo($item, PATHINFO_FILENAME); return (stristr($fn, $search)); });


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?


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

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

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

// close directory

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

// sort 'em

// print 'em
print("<TABLE border=1 cellpadding=5 cellspacing=0 class=whitelinks>\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>");

Be a part of the DaniWeb community

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