MG lolenstine
MG lolenstine

Reputation: 969

Showing off a HUGE Text file on a website

Ok, so I have a huge text file (around 2 gigabytes) and it has about 2 billion lines. All I've tried so far is

$myfile = fopen("C:\Users\server\Desktop\primes.txt", "r") or die("Unable to 
open file!");
while(!feof($myfile)) {
    echo fgets($myfile) . "<br>";
}
fclose($myfile);

but has a problem with not finishing all of the lines and hangs up somewhere in the first quarter - and it also takes a long time to load. Second thing I've tried was this

$path="C:/Users/server/Desktop/Server files/application.windows64/";
$file="primes.txt";

//read file contents
$content="
        <code>
            <pre>".file_get_contents("$path/$file")."</pre>
        </code>";

//display
echo $content;

But it wasn't even loading lines.

Also, I can't directly open this file and it has to be on my Desktop. Please don't suggest me to copy or move it into another directory.

Could I get any suggestions to help me get along or at least an explanation why it's not working?

Sorry, my English isn't as good as it should be.

Upvotes: 1

Views: 235

Answers (1)

thomasmeadows
thomasmeadows

Reputation: 1881

   $attachment_location = $_SERVER["DOCUMENT_ROOT"] . $file;
    if (file_exists($attachment_location)) {

        header($_SERVER["SERVER_PROTOCOL"] . " 200 OK");
        header("Cache-Control: public");
        header("Content-Type: text/plain");
        header("Content-Length:" . filesize($attachment_location));
        header("Content-Disposition: attachment; filename=file.txt");
        readfile($attachment_location);
        die();        
    } else {
        die("Error: File not found.");
    } 

It's not working because it's a 2 gig file and you're trying to output it to the screen as opposed to allowing the user to download it or open it on their personal machine. That should be the only way this file is delivered. 2 gigs output to a browser window would probably crash either or both of the client and server.

http://php.net/manual/en/function.readfile.php

If you really want to actually display the file, it would technically be possible to display a certain % of the file with a pager that when clicked will switch between different portions of the file.

http://php.net/manual/en/function.fseek.php

Upvotes: 3

Related Questions