Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7551365
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T10:28:32+00:00 2026-05-30T10:28:32+00:00

I have created a piece of code that checks files for a user-submitted string

  • 0

I have created a piece of code that checks files for a user-submitted string within a set of files. The code searches a directory, returns the file, then searches for the string in the file. The user will input the custom string through an input field and then clicking a submit button.

I have successfully been able to create a condition where, if the user does not enter any information, the output will say, “Your search produced no results”. However, I have not been able to figure out how to create a condition where, if the user enters a string that isn’t found within the files, that the output will also be, “Your search produced no results”.

The code for the existing conditional I have as of now is this:

if ((isset($query)) && (empty($query))) {
    echo "Your search produced no results";
}

The code that searches for the files and also searches for the string is found here (this is the entire PHP file, actually, and it includes the conditional I posted above.) I need help on how to create another conditional that throws a message if the user-input isn’t found in any of the files.

If this seems unclear, I apologize and will clarify any information you need if you think it will help.

Calling Code

$query = $_POST['query'];

if ((isset($query)) && (empty($query))) {
    echo "Your search produced no results.";

}
else {
    find_files('.');
}

find_files()

function find_files($seed)
{
    if (! is_dir($seed))
        return false;

    $files = array();
    $dirs = array($seed);

    while(NULL !== ($dir = array_pop($dirs)))
    {
        if($dh = opendir($dir))
        {
            while( false !== ($file = readdir($dh)))
            {
                if($file == '.' || $file == '..') continue;

                $path = $dir . '/' . $file;

                if (is_dir($path)) {
                    $dirs[] = $path;
                }
                else {
                    if (preg_match('/^.*\.(php[\d]?|js|txt)$/i', $path)) { 
                        check_files($path);
                    }
                }
            }
        }

        closedir($dh);
    }
}

check_files()

function check_files($this_file) 
{
    $query = $_POST['query'];

    $str_to_find = $query;

    if(!($content = file_get_contents($this_file))) {
        echo("Could not check $this_file");
    }
    else {
        if (stristr($content, $str_to_find)) {
            echo("$this_file -> contains $str_to_find");
        }
    }

    unset($content);
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T10:28:34+00:00Added an answer on May 30, 2026 at 10:28 am

    If the query is not empty the find_files function is simply executed with no instructions of doing something if it returns false, hence you need to evaluate the result of calling find_files. For example you could do:

    if ((isset($query)) && (empty($query))) {
        echo "<p style=\"color:darkgray; font-family:arial\">Your search produced no results</p>";
    }
    elseif (!find_files('.'))
    {
        echo "<p style=\"color:darkgray; font-family:arial\">Your search produced no results</p>";
    }
    

    with the condition that you update you find_files function to return false for all cases that fail.

    or you could update the find_files function to return a string in case of errors and a string empty for succesful execution

    if ((isset($query)) && (empty($query))) {
        echo "<p style=\"color:darkgray; font-family:arial\">Your search produced no results</p>";
    }
    else 
    {
        $result = find_files('.');
        if (!empty($result))
        {
            echo "<p style=\"color:darkgray; font-family:arial\">".$result."</p>";
        }
    }
    

    A couple of notes regarding your code that will improve the readability and code quality:

    • proper indentation will save a lot of time spent in maintenance;
    • when using if else imbrications ALWAYS use curly braces even if it is only one instructions. Improves readability and avoids errors.
    • when accessing a variable declared outside a function (in procedural code) use global keyword. For example for accessing the query variable inside the check_files function use global $query; instead of retrieving the variable again from the post.
    • use $_REQUEST instead of $_POST or $_GET unless there is a special reason for doing otherwise. Unifies code, makes for more readable code and changing from GET to POST or vice-versa can be done without changing code.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this piece of code that basically checks if one or more files
I have a piece of code here that breaks if the directory doesn't exist:
I have the following piece of code that checks if the returned video object
I have a piece of code that reads hell of a lot (hundreds of
I have a piece of code that creates an SQL Server Express 2008 in
I've got a short piece of code that originally created an SqlDataAdapter object over
I have created a benchmark class that allows the user to insert for example
I have created a task in Symfony 1.4 that loads in some CSV files
In my program I have currently a piece of code that looks like this
I have created a rather complicated piece of (Java) code for a game I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.