PHP检查文件是否包含字符串

时间:2012-01-30 03:33:04

标签: php fopen strpos

我正在尝试查看文件是否包含发送到页面的字符串。我不确定这段代码有什么问题:

?php
    $valid = FALSE;
    $id = $_GET['id'];
    $file = './uuids.txt';

    $handle = fopen($file, "r");

if ($handle) {
    // Read file line-by-line
    while (($buffer = fgets($handle)) !== false) {
        if (strpos($buffer, $id) === false)
            $valid = TRUE;
    }
}
fclose($handle);

    if($valid) {
do stufff
}

5 个答案:

答案 0 :(得分:73)

更简单:

<?php
    if( strpos(file_get_contents("./uuids.txt"),$_GET['id']) !== false) {
        // do stuff
    }
?>

回应有关内存使用情况的评论:

<?php
    if( exec('grep '.escapeshellarg($_GET['id']).' ./uuids.txt')) {
        // do stuff
    }
?>

答案 1 :(得分:17)

搜索较大的文件时,代码更有效。

$handle = fopen('path_to_your_file', 'r');
$valid = false; // init as false
while (($buffer = fgets($handle)) !== false) {
    if (strpos($buffer, $id) !== false) {
        $valid = TRUE;
        break; // Once you find the string, you should break out the loop.
    }      
}
fclose($handle);

答案 2 :(得分:3)

function getDirContents($dir, &$results = array())
{

    if ($_POST['search'] == null)
        exit;

    ini_set('max_execution_time', $_POST['maxtime']);

    $_SESSION['searchString'] = $_POST['search'];

    echo "<script>var elm = document.getElementById('search');elm.value='$_POST[search]';</script>";

    if (!isset($_POST['case']))
        $string = strtolower($_POST['search']);
    else
        $string = $_POST['search'];
    $files = scandir($dir);

    foreach ($files as $key => $value) {
        $path = realpath($dir . DIRECTORY_SEPARATOR . $value);
        if (!is_dir($path)) {
            $content = file_get_contents($path);
            if (!isset($_POST['case']))
                $content = strtolower(file_get_contents($path));
            if (strpos($content, $string) !== false) {
                echo $path . "<br>";
            }
            $results[] = $path;
        } else if ($value != "." && $value != "..") {
            getDirContents($path, $results);
            $results[] = $path;
        }
    }
    return $results;
}

原创项目:https://github.com/skfaisal93/AnyWhereInFiles

答案 3 :(得分:1)

这是正常的,我已经对端到端进行了测试。

<?php
// incoming record id 
// checking in uuids.txt file
if (exec('cat ./uuids.txt | grep '.escapeshellarg($_GET['id']))) {
     // do stuff
     echo 'found...';
} 
?>

答案 4 :(得分:-1)

<?php
    function getDirContents($dir, &$results = array()){
        $files = scandir($dir);
        foreach($files as $key => $value){
            $path = realpath($dir.DIRECTORY_SEPARATOR.$value);
            if(!is_dir($path)) {
                $results[] = $path;
            } else if($value != "." && $value != "..") {
                getDirContents($path, $results);
                $results[] = $path;
            }
        }
        return $results;
    }
    $res = getDirContents('path');
    $searchfor = 'search string';
    foreach ($res as $file) {
        if(is_dir($file)) {}else{
            $contents = file_get_contents($file);
            $pattern = preg_quote($searchfor, '/');
            $pattern = "/^.*$pattern.*\$/m";
            if(preg_match_all($pattern, $contents, $matches)){ ?>
                <tr>
                    <td> <?php $string = implode("\n", $matches[0]); echo str_replace($searchfor,'<strong style="background-color:#ffff00">'.$searchfor.'</strong>',$string); ?> </td>
                    <td> <?php echo  $file; ?> </td>
                </tr>
            <?php }else{
                //echo "No matches found";
            }
        }
    }
?>