View Full Version : How to open huge file so as to break it up?

04-22-2003, 06:36 AM
I have a file of about 500,000 records. If I try to open it with file("myfile.txt"), I get a page cant be displayed error. I assume it chokes on size. I want to chop the file into smaller pieces. How can I do this if I cannot even open the file?


04-22-2003, 10:34 AM
you'd probably want to use fopen, fgets and fclose to read the file a line at a time

note: you may well need to raise your max_execution_time with an ini_set() call or recall the same script multiple times and adjust the start point with fseek and ftell

dodgy untested example

$fsk = (isset($_GET['fsk'])) ? $_GET['fsk'] : 0;
$fp = fopen('filename.ext');
while($f_line = fgets($fp, 4096))
// process the line here
// adjust the 200000 below to set read size
if(ftell($fp) > ($fsk + 200000))
// 200KB processed - refresh page
header("location: ............?fsk=" .ftell($fp));