user4688302
user4688302

Reputation: 1

php search in a specified folder

I'm trying to write a PHP function that searches for a file in a specified folder. The if function doesn't work properly both blocks of code get executed, here's the code.

 <?php

$dir = "C:/xampp/htdocs/phpsearchbox/forms";
$dirtest = "C:\xampp\htdocs\phpsearchbox\forms" ;
$dh  = opendir($dir);


while (false != ($filename = readdir($dh))) {
$files[] = $filename;}

$indexCount = count($files);

for( $index=0 ; $index<$indexCount ; $index++)

{    $post_path = $dir . $files[$index];
     $post_patharray []= $post_path;
     $post_pathtest=$dirtest.$files[$index];
    $filesize= number_format(filesize($files[$index]));
    $filesizearray []= $filesize;

$wantedfile = substr($post_patharray[$index], 34 , -4);
$wantedfilearray [] = $wantedfile;
$wantedfile1 = substr($post_patharray[$index], 34);
$wantedfile1array [] = $wantedfile1;
$filetype = substr($post_patharray[$index], -3);

$searchbox = $_POST['search'];
$searchResult = array_search($searchbox, $files,false);

         if($searchbox == $wantedfilearray[$index] )
{           
             echo("<table border='0' width='100%' cellspacing='-90' > <br>");
             echo ("<tr align='left'>");
             echo('<TD bgcolor="#737374" width="12.9%">');
             echo("<div style='position: relative'");
             echo ("<p size='0.5'>$wantedfile1array[$index]</p>");
             echo ("</div></td>");
             echo ('<td bgcolor="#737374" width="38%">');
             echo("<div style='position: relative'");
             echo ("<p size='1'><a href=\"$files[$index]\"  target='_blank' > C:\ xampp\ htdocs \ phpsearchbox \ forms \ $files[$index]</a><p>");
             echo ("</div></td>");
             echo('<td text-align="justify" bgcolor="#737374" width="15%"> ');
             echo('<div ');
             echo ("<p>$filetype</p>");
             echo("</div></td>");
             echo('<td bgcolor="#737374" width="15%">');
             echo("<div style='position: relative'");
             echo("<p> $filesizearray[$index]</p>");
             echo("</div></td>");
             echo("</TR>\n");
             echo ("</table>");

}
else
{echo ("<br><br><b><font face='Arial' size='2'>sorry! no documents found, please try another ID</font></b>");
}

}

?>

Upvotes: 0

Views: 73

Answers (2)

Adam Link
Adam Link

Reputation: 2793

The issue is your for block. Your for block starts as 0. Your $indexCount starts at 1 if a file is returned and will increase for each file that is found in the $dh directory.

You then loop over each file and compare the names, echoing if the file found matches the name or saying there were no files found. I'd guess that your directory has 2 files in it. One matches, and fires the if statement. The other doesn't match, and fires the else statement. It appears that both are firing - and they are - though because there are two different files, not because you have both blocks firing for the same file.

EDIT

Using your code above, I'd do the following:

class FileSearch {
    const DIR = "C:/xampp/htdocs/phpsearchbox/forms";
    const DIR_TEST = "C:\xampp\htdocs\phpsearchbox\forms";

    public function search($searchbox) {
        $return = null;
        $dh  = opendir(self::DIR);

        $files = [];
        while (false != ($filename = readdir($dh))) {
            $files[] = $filename;
        }

        $indexCount = count($files);

        for( $index = 0; $index < $indexCount; $index++) {
            $post_path = self::DIR . $files[$index];
            $post_patharray[] = $post_path;
            $post_pathtest = self::DIR_TEST.$files[$index]; 
            $filesize = number_format(filesize($files[$index]));
            $filesizearray[] = $filesize;

            $wantedfile = substr($post_patharray[$index], strlen(self::DIR) , -4);
            $wantedfilearray[] = $wantedfile;
            $wantedfile1 = substr($post_patharray[$index], strlen(self::DIR));
            $wantedfile1array[] = $wantedfile1;
            $filetype = substr($post_patharray[$index], -3);

            $searchResult = array_search($searchbox, $files,false);

            if($searchbox == $wantedfilearray[$index] ) {
                $return .= "<table border='0' width='100%' cellspacing='-90' > <br>";
                $return .= "<tr align='left'>";
                $return .= '<TD bgcolor="#737374" width="12.9%">';
                $return .= "<div style='position: relative'";
                $return .= "<p size='0.5'>$wantedfile1array[$index]</p>";
                $return .= "</div></td>";
                $return .= '<td bgcolor="#737374" width="38%">';
                $return .= "<div style='position: relative'";
                $return .= "<p size='1'><a href=\"$files[$index]\"  target='_blank' > C:\ xampp\ htdocs \ phpsearchbox \ forms \ $files[$index]</a><p>";
                $return .= "</div></td>";
                $return .= '<td text-align="justify" bgcolor="#737374" width="15%"> ';
                $return .= '<div ';
                $return .= "<p>$filetype</p>";
                $return .= "</div></td>";
                $return .= '<td bgcolor="#737374" width="15%">';
                $return .= "<div style='position: relative'";
                $return .= "<p> $filesizearray[$index]</p>";
                $return .= "</div></td>";
                $return .= "</TR>\n";
                $return .= "</table>";
            } else {
                $return =  "<br><br><b><font face='Arial' size='2'>sorry! no documents found, please try another ID</font></b>";
            }
        }

        return $return;
    }

}

$mySearcher = new FileSearch();
echo $mySearcher->search($_POST['search']);

You should separate your view from your controller - meaning that you shouldn't be coding in the HTML into your PHP function. You should simply return the needed data from your function and then format it as needed in your page. For more info, see this SitePoint MVC framework tutorial

Upvotes: 0

gvgvgvijayan
gvgvgvijayan

Reputation: 2506

$dir = 'E:\\';
echo '<pre>';
foreach (glob($dir . '*.dll') as $filename) {
    echo $filename, '<br/>', 'PROPERTIES:<br/>';
    print_r(stat($filename));
}

You can simply implement this logic using Glob and Stat function

GLOB:

  • whose first parameter is the search pattern (according to libc pattern matching)
  • second parameter is list of flags

    • GLOB_MARK - Adds a slash to each directory returned
    • GLOB_NOSORT - Return files as they appear in the directory (no sorting). When this flag is not used, the pathnames are sorted alphabetically
    • GLOB_NOCHECK - Return the search pattern if no files matching it were found
    • GLOB_NOESCAPE - Backslashes do not quote metacharacters
    • GLOB_BRACE - Expands {a,b,c} to match 'a', 'b', or 'c'
    • GLOB_ONLYDIR - Return only directory entries which match the pattern
    • GLOB_ERR - Stop on read errors (like unreadable directories), by default errors are ignored.

STAT:

  • Which give information or properties of a file both in numerical and associative index

also stat's value are depend on your operating system especially if you use windows read documentation clearly where it comes 0 and -1.

Upvotes: 1

Related Questions