Jump to content


Photo

Php Reading A Txt File


  • Please log in to reply
2 replies to this topic

#1 CanMan2004

CanMan2004
  • Members
  • PipPipPip
  • Advanced Member
  • 254 posts

Posted 28 June 2006 - 01:12 PM

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

#2 alexruimy

alexruimy
  • Members
  • PipPip
  • Member
  • 20 posts

Posted 28 June 2006 - 01:37 PM

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

Try this:

<?
$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;
?>


#3 CanMan2004

CanMan2004
  • Members
  • PipPipPip
  • Advanced Member
  • 254 posts

Posted 28 June 2006 - 02:12 PM

You're a star, thank you




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users