JewGold Posted August 4, 2008 Share Posted August 4, 2008 I'm trying to create a script that will take user-defined data from an html form, write it to a local text file, then display the text files contents, size of the text file, and the number of lines, all in the browser. This script is really of no use other then to practice my file reading/writing. Now I have everything working, its just I cannot for the life of me figure out how to read the text file and output how many lines it has. No clue at all. This is my best try: $amount=0; $fp=fopen("/home/user/Desktop/test.txt", 'r'); while ( ! feof($fp)) { $line = fgets( $fp, 1024 ); $amount++; } print $amount; I'm still very much new to PHP and programming in general, so the answer is probably staring at right at me. Am I on the right track with this? Link to comment https://forums.phpfreaks.com/topic/118000-im-having-trouble-using-fgets-and-feofs-through-loops/ Share on other sites More sharing options...
coder500 Posted August 4, 2008 Share Posted August 4, 2008 The following code will display the number of lines in a .txt file <?php $file = "welcome.txt"; $lines = count(file($file)); echo "There are $lines lines in $file"; ?> Link to comment https://forums.phpfreaks.com/topic/118000-im-having-trouble-using-fgets-and-feofs-through-loops/#findComment-607227 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.