Jump to content


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


Php Reading A Txt File

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


Share this post

Link to post
Share on other sites
Assuming the exact contents of your log.txt file are
[code]username - Dave Smith
age - 34[/code]

Try this:

$file = "log.txt";
$size = filesize($file);
$fh = fopen ($file, 'r');
$data = fread ($fh, $size);
$lines = explode("\n",$data);
$pieces = explode(" - ",$lines[0]);
$username = $pieces[1];
echo $username;

Share this post

Link to post
Share on other sites


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.