0

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

3
Contributors
5
Replies
8
Views
4 Years
Discussion Span
Last Post by navi18
0

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.

Edited by minitauros

0

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..

0

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);

Edited by diafol

0

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?

0

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>
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.