Read content from multiple .txt files in a given folder

2

I have this code that reads a file created by Netscape HTTP Cookie File , however I was only able to use the script and read the file of the user who is with the session username open, I need to list all TXTS and read all they without exception.

NOTE: The files have different names:

<?php

require_once getcwd() . '/modules/config.php';

$username = null;

if (isset($_SESSION['username'])) {
    $username = $_SESSION['username'];
}

$pointer = fopen(getcwd() . '/cookies/' . $username . '.txt', 'r');

while (!feof($pointer)) {
    $row = fgets($pointer);
}

fclose($pointer);

How to read all files with different names other than the file with the name created in the session?

EDITED

In the comments Anderson cited the use of the glob() function, I was able to read all the files with this code.

foreach (glob(getcwd() . '/cookies/*.txt') as $file) {
    $pointer = fopen($file, 'r');
    while (!feof($pointer)) {
        $row[] = fgets($pointer);
    }
}

var_dump($row);

But it contains unnecessary lines in the file, I need to get only the cookies , look what returns me:

I need only cookies , understand?

    
asked by anonymous 04.09.2017 / 16:05

1 answer

2

By comment , you only want to catch the rows that contain the _twitter_sess , ct0 , and auth_token values. You can store these values in a list:

$tokens = ['_twitter_sess', 'ct0', 'auth_token'];

And check which rows have at least one of these values:

foreach (glob(getcwd() . '/cookies/*.txt') as $file) {
    $pointer = fopen($file, 'r');
    while (!feof($pointer)) {
        $line = fgets($pointer);

        foreach ($tokens as $token) {
            if (strpos($line, $token) !== false) {
                $row[] = $line;
                break;
            }
        }
    }
}

Thus, the line in $pointer will only be added to the list if any token set on $tokens is found on the line. After that, you can use both regular expression and separate the line in the white space to extract the desired value.

    
04.09.2017 / 17:13