Jump to content

Php Reading A Txt File


CanMan2004

Recommended Posts

Hi all

I have a site which creates a log txt file for me, inside it, it stores the name of the person who logged in and there age, for example the txt file might look like

username - Dave Smith
age - 34

How can I get php to open the file and read the username piece of information inside the txt file and ignore the rest? I have read some information on reading files but all my examples seem to fail.

Any help would be great

Thanks in advance

Dave
Link to comment
https://forums.phpfreaks.com/topic/13107-php-reading-a-txt-file/
Share on other sites

Assuming the exact contents of your log.txt file are
[code]username - Dave Smith
age - 34[/code]

Try this:

[code]<?
$file = "log.txt";
$size = filesize($file);
$fh = fopen ($file, 'r');
$data = fread ($fh, $size);
fclose($fh);
$lines = explode("\n",$data);
$pieces = explode(" - ",$lines[0]);
$username = $pieces[1];
echo $username;
?>[/code]
Link to comment
https://forums.phpfreaks.com/topic/13107-php-reading-a-txt-file/#findComment-50395
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.