Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Some text file questions.....

Status
Not open for further replies.

Naits

Programmer
Oct 10, 2001
90
NO
Hi!
1. How to get num of lines in a .txt file?
2. How to read just one line from a .txt file? __________________
Visit my homepage
.: Game universE :.
 
Quick and dirty but it'll work for you:

Write to file:

<?php


$fn = &quot;C:/text/FileTest2.txt&quot;;

$text = &quot;Appended to file:\n&quot;;
$file = @fopen($fn, &quot;w+&quot;) or die(&quot;Unable to open file&quot;);

@fwrite($file, $text) or die(&quot;Write to file failed.&quot;);
$msg = &quot;Write to file succeeded&quot;;
fclose($file);
?>

<html>
<head>
<title>File Write</title>
</head>
<body>
<?php echo $msg; ?>
</body>
</html>

--------------------
Read from file
<?php
$file = &quot;C:/text/FileTest2.txt&quot;;
$fp = fopen( $file, &quot;r&quot; );
$data = nl2br(fread( $fp, filesize( $file)));
$data = explode(&quot;:&quot;, $data );
$len = count( $data )-1;
fclose( $fp );
//echo $data[1];
echo 'Number of lines in file are: ' . $len. '<BR>';
for( $x = 0; $x < $len; $x++)
{
echo $data[$x];
}
?>
--------------------
 
For this purpose, the file(string filename) function comes in very handy. For example
<?php
$linearray = file(&quot;filename&quot;);
echo &quot;There are &quot; . sizeof($linearray) . &quot; lines in filename.&quot;;
$line14 = $linearray[13];
echo &quot;Line 14 contains: $line14&quot;;
?> //Daniel
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top