Jump to content

Recommended Posts

Hello,

I found many free scripts on the net to make file search , but I dont know which is better can you suggest one , I have no time to make one myself

 

Note:

I need it to search specific folder but also go to sub folder and to find based on limited number of extensions

 

thanks alot

Link to comment
https://forums.phpfreaks.com/topic/67342-file-search-ready-to-use-script/
Share on other sites

If you want someone to do this for you, i suggest you try the freelance forum. This forum is to help you with the things that you are attempting to do.

 

If you do want some help, then take a look at the following bit of code:

 

<?php
function no_of_files($start_dir,$sub_dir=FALSE){
    $no_of_files=0;
    if($sub_dir === false){
        $handler = opendir($start_dir);	
    }else{
        $start_dir = $start_dir.$sub_dir;
        $handler = opendir($start_dir);
    }

    while(false !== ($file = readdir($handler))){
        if($file != '.' && $file != '..'){
            if(is_dir($start_dir.'\\'.$file)){//if this is a directory, recall the function with the subdirectory defined
                $dir_name = '\\'.$file;
                echo $dir_name.'<br />';
                $no_of_files += no_of_files($start_dir,$dir_name);	
            }else{//is a file, so increase our counter
                //here is where you would check to see if the file is the one you are looking for
                $no_of_files++;	
            }	
        }
    }
    return $no_of_files;
}
echo no_of_files('C:\Documents and Settings\Ben\My Documents\My Music');
?>

 

That was a function i wrote to count the number of files in a given folder and all its subfolders. It wouldn't be too hard to modify the function to accept a 3rd parameter - the file you are searching for. It would then be a case of adding in a test to see if the file currently being looked at in the loop is the one you are after.

As i said, the function i posted doesn't already do what you're trying to do. I was basically pointing you in the right direction as far as a recursive function is concerned(your function needs to call itself to allow you to search through an undefined level of subdirectories) You'll need to modify it. How you modify it rather depends on how you want it to work.

 

You could simply set it up to echo something when it finds the file. That would be the easiest modification, though perhaps least useful. I would personally alter what the function returns; i woud have it return true on a match, and false with no match (if you take this method, you'll need to add an if statement to test the result of the function where it is called by itself)

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.