Click here to Skip to main content
15,886,519 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, I'm new to php and need help.

I'm trying to search files on the basis of their content in which I got success.
for that I'm running a function in php as follows and showing result table.

PHP
<!--searchFileContents CLASS-->
<?php
#class for keyword Search 
class searchFileContents{
	#Declarations of variables
    var $dir_name = '';//The directory to search (root)
    var $search_phrase = '';//The phrase to search in the file contents
	
    var $allowed_file_types = array('txt', 'htm', 'html');//The file types that are searched
    
    var $foundFiles;//Files that contain the search phrase will be stored here
    var $myfiles;// get all the files in all the directories
	    
	#function to search  the phrase with input parameters like 
	#directory name and the phrase to search
    function search($directory, $search_phrase)
	{
        $this->dir_name = $directory;//create a local var for directory
        $this->search_phrase = $search_phrase;// create a local var for phrase
        
		# get all the files in all the directories
		$rDirIter = new RecursiveDirectoryIterator($this->dir_name, FilesystemIterator::SKIP_DOTS);
		$this->myfiles = new RecursiveIteratorIterator ( $rDirIter);//get all the files using the recursive method of php 
		
		#array declaration
        $this->foundFiles = array();
        
		//if search keyword/phrase is empty this code will run
        if ( empty($this->search_phrase) ) die('Please type something to search!');
		
		//if directory is empty this code will run
        if ( empty($this->dir_name) ) die('You must select a directory to search');
        
		#Search the keyword inside the files
        foreach ( $this->myfiles as $f )//get all the files in $f
		{
			#in_array() will check if $allowed_file_types has the extension or not
			#explode() will convert the path into array
			#array_pop() will Pop the element off the end of array, here extension of file
            if ( in_array(array_pop(explode ( '.', $f )),  $this->allowed_file_types) )// search the allowed files only
			{
                $contents = file_get_contents($f);//search the keyword inside the files
                if ( stripos($contents, $this->search_phrase) !== false )					
                    $this->foundFiles [] = $f;					
            }//end of if
        }//end of for
        return $this->foundFiles;
    }//end of function
}//end of class searchFileContents	
?>


which I'm calling @

PHP
<?php	
#Search the keyword 
if($_POST['Submit']=="Submit") 
{ //a search request was made display my search results 
	$SearchQuery = $_POST['txt_query']; #Search keyword 
	$TestPath="C:/intranet/www/Documnet_Search"; 	
	$search = new searchFileContents; 
	$search->search($TestPath, $SearchQuery); 
	
	$narray = array();
	$narray = $search->foundFiles;// use the array to show the result


Now my search is getting bigger as new files are added and now its taking time.
Could anyone please help me to implement a Progress bar to let the user know that the search in still in progress?
Posted
Comments
GoneBump 9-Sep-14 5:59am    
I tried a the following code but it loads at the page load and not efficient:
<pre lang="PHP">

<div id='progress' style='width:500px;border:1px solid #ccc;'></div>
<!-- Progress information -->
<div id='information' style='width:20px'></div>
";
?>

document.getElementById("progress").innerHTML="<div style=\"width:'.$percent.';background-color:#ddd;\"> </div>";
document.getElementById("information").innerHTML="'.$i.' row(s) processed.";
</script>';


// This is for the buffer achieve the minimum size in order to flush data
echo str_repeat(' ',1024*64);


// Send output to browser immediately
flush();


// Sleep one second so we can see the delay
sleep(1);
}
// Tell user that the process is completed
echo '<script language="javascript">document.getElementById("information").innerHTML="Process completed"</script>';
?>
</pre>
vbmike 9-Sep-14 9:35am    
Not sure as I have not done the progress bar thing but PHP addresses it somewhat: http://php.net/manual/en/session.upload-progress.php, again not sure if this will help but may be more info on the page related to your issue....
GoneBump 10-Sep-14 8:17am    
Thanks vbmike but this will be only used for upload system over the session. not for processing a function.

1 solution

You could check the following link it is a progress bar made in php itself.

http://vancewalsh.com/php/[^]
 
Share this answer
 
Comments
GoneBump 12-Sep-14 3:00am    
Thanks

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900