script to read large files

Ask about general coding issues or problems here.

Moderators: egami, macek, gesf

Post Reply
robbeen
New php-forum User
New php-forum User
Posts: 2
Joined: Fri Jun 15, 2018 11:36 am

Fri Jun 15, 2018 11:44 am

Hi Team,

PHP 5.4

I have developed a form with a textfiled and button. The user will enter a number in the textfiled and click on submit.
$num = $_POST['number'];
$file="largefile.txt";

The script will count the total number of lines in a file and save in a variable
// The code below counts the total number of lines in the file
$file = "largefile.txt";
$lines = count(file($file));

My requirement is I want to divide the $lines with the $num and the result will be the number of files created by php which will include $num records.

How can I achieve this?

I was planning the use a while loop to read the entire file and a for loop inside the while loop

$file = fopen("largefile.txt","r");
while(! feof($file))
{
echo fgets($file). "<br />";
}

fclose($file);

Regards,

Joe

Post Reply