Read contents of multiple. txt’s files from a given folder

Asked

Viewed 613 times

1

I have this code that reads a file created by Netscape HTTP Cookie File, however I could only use the script and read the file of the user who is with the session username open, I need to list all the TXTS and read them all 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 function glob(), 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);

However it contains unnecessary lines in the file, I need to get only the cookies, look what comes back:

inserir a descrição da imagem aqui

I just need the cookies, you know what I mean?

  • 1

    Read about the function glob.

  • Ta blz. I’ll see.

  • @Andersoncarloswoss can answer my own question?

  • 1

    Yes, you can, no problem.

  • @Andersoncarloswoss I edited, see if you can help me.

  • 1

    Which of these lines would be cookies?

  • _twitter_sess, ct0, auth_token, twid, dessses, prefix and values. understands?

Show 2 more comments

1 answer

0


For commenting, you want to pick up only the lines that contain the values of _twitter_sess, ct0 and auth_token. You can store these values in a list:

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

And check which lines 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 some token defined in $tokens is found on the line. After that, you can use either regular expression or separate the line in the blanks to extract the desired value.

  • Testing... just a minute, you’re in error, I’ll try to solve

  • 1

    Okay, you had to read the file line before the check, with the variable $line. Before I was checking the pointer, which doesn’t make much sense.

  • I added more lines, $tokens = ['_twitter_sess', 'ct0', 'auth_token', 'twid']; but it’s only returning me like this: http://prntscr.com/ggz52e, and I realized you’re not listing all the files.

  • You checked whether the return of glob is the list of all files?

  • Yes they all contain in the file, in case there you are checking all that are different from false in case, return me all the lines that are TRUE / TRUE but it’s not all coming back, just _twitter_sess, the first two, file 1 and file 2. And the rest not.

  • Is there any way to put the contents of the files somewhere so we can replicate? Can remove potentially sensitive data as long as it does not change the file format.

  • @Pedrosilva another mistake of mine, pardon. O fgets must be outside the loop that runs through the tokens, otherwise the content of the line is changed from a token for the other, so it generated an unexpected result. I already changed the answer and I believe that it should work.

  • Good, I could have noticed this too, but passed rsrs smoothies, thank you, now a doubt, I can use these cookies normally without using the global $_COOKIES ? it makes no sense to save a file containing cookies and not use them.

Show 3 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.