How do I read each line from a file in php?

I'm new to learning php and in one of my first programs I wanted to make a basic php website with login capabilities with and array of the user and passwd.

my idea is to store the username as a list parameter and have the passwd as the contents, like this:

arr = array(username => passwd, user => passwd);

now my problem is that I don't know how I can read from the file (data.txt) so I can add it into the array.

data.txt sample:
username passwd
anotherUSer passwd

I've opened the file with fopen and stored it in $data.

Answers


You can use the file() function.

foreach(file("data.txt") as $line) {
    // do stuff here
}

Modify this PHP example (taken from the official PHP site... always check first!):

$handle = @fopen("/path/to/yourfile.txt", "r");
if ($handle) {
    while (($buffer = fgets($handle, 4096)) !== false) {
        echo $buffer;
    }
    if (!feof($handle)) {
        echo "Error: unexpected fgets() fail\n";
    }
    fclose($handle);
}

to:

$lines = array();
$handle = @fopen("/path/to/yourfile.txt", "r");
if ($handle) {
    while (($buffer = fgets($handle, 4096)) !== false) {
        lines[] = $buffer;
    }
    if (!feof($handle)) {
        echo "Error: unexpected fgets() fail\n";
    }
    fclose($handle);
}

// add code to loop through $lines array and do the math...

Be aware that you should not store login details in a textfile that in addition is not encrypted, this approach has severe security issues. I know you are new from PHP, but the best approach is to store it in a DB and crypting the passwords with an algorithm such as MD5 or SHA1,


You shouldn't store sensitive information as plaintext, but to answer your question,

$txt_file = file_get_contents('data.txt'); //Get the file
$rows = explode("\n", $txt_file); //Split the file by each line

foreach ($rows as $row) {
   $users = explode(" ", $row); //Split the line by a space, which is the seperator between username and password
   $username = $users[0];
   $password = $users[1];
}

Take a look at this thread.


This works for extremely large files as well:

$handle = @fopen("data.txt", "r");
if ($handle) {
    while (!feof($handle)) { 
        $line = stream_get_line($handle, 1000000, "\n"); 
        //Do Stuff Here.
    } 
fclose($handle);
}

Use file() or file_get_contents() to create either an array or a string.

process the file contents as needed

// Put everything in the file in an array
$aArray = file('file.txt', FILE_IGNORE_NEW_LINES);

// Iterate throug the array
foreach ($aArray as $sLine) {

    // split username an password
    $aData = explode(" ", $sLine);

    // Do something with the username and password
    $sName = $aData[0];
    $sPass = $aData[1];
}

Need Your Help

Expand gtkmm GtkTreeView column

c++ gtkmm gtktreeview

Im looking forward to expand one of my gtkmm treeview columns so it would use all the space left, and shrink other columns... Its there any way to do this?

how to extract colored regions from original image

matlab image-processing extraction image-segmentation

I applied the srm Algorithm on images to smooth the colors for each object, in the image in order to extract it , then I called the function extractLabel to exctract each colored region in a separate