简体   繁体   中英

Grabbing data from a text file in a “line per line” manner - and sending those into mysql using PHP

Ok I have a data similar like this, written in a single text file..

BOX,12

CAN,99

.... and so on.

Now I want to execute those into mysql tables using explode() what will not be a problem.

But anyway, any ideas how can I grab row per row (line per line) from a text file into PHP?

You can do this:

// Trying to open TXT file
if( $file = fopen('file.txt', 'r') )
{
    // Loop until the End Of File
    while( ! feof($file) )
    {
        // Get current line
        $line = fgets($file);

        echo $line . '<br />';
    }

    // Closing TXT file
    fclose($file);
}
else
    echo 'fopen() fail';

For more detais about the functions:

fopen()
feof()
fgets()
fclose()

Read it like this:

$lines = file('my/file/text.txt');

foreach ($lines as $line_num => $line) {
    echo "Line #<b>{$line_num}</b> : " . $line . "<br>\n";
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM