-2

I am trying to read a huge file (6 million lines, 80mb) using this syntax in PHP :

readfile("output.data");

I have to read this file and display only few lines of data from this huge file, now my problem is my WAMP server is hanged whenever I include this line (readfile("output.data"); ) in my php code,

James Z
  • 12,209
  • 10
  • 24
  • 44
EngineerAkki
  • 89
  • 1
  • 9
  • Better for [the code review site](http://codereview.stackexchange.com/) – Can O' Spam Oct 12 '15 at 09:16
  • 2
    @SamSwift this is absolutely **not** appropriate for Code Review. Rule #1 of CR is that code must be already working as intended. – Kaz Oct 12 '15 at 09:21
  • @Zak, this code does work, it is just slow, so a **review** to the code needs to be made to allow for speed and accuracy, could also say it is working as intended, the intention is to read the file, which the OP can do, therefore the code **is working as intended but requires reviewing** – Can O' Spam Oct 12 '15 at 09:22
  • 2
    @SamSwift While Code Review can review code for performance optimisations, the problem here is not that the code runs too slowly, but the code does not finish running at all. That is firmly in the realm of debugging (as evidenced by the wording of the question e.g. "Please help me with this issue"). – Kaz Oct 12 '15 at 09:33
  • And at any rate, there is simply not enough code here to review.. This is not a reviewable question. This appears to be an issue with OP trying to pull too much data from a large file into memory at once instead of streaming it, but it's definitely a debugging issue – Dan Oct 12 '15 at 09:34
  • 1
    You should read file line by line. Please look at this answer http://stackoverflow.com/a/13246630/1866988 – Fedir Petryk Oct 12 '15 at 10:06

1 Answers1

0

Ever tried jQuery ajax?

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script>
    $( document ).ready( function ()
    {
        $.ajax( {
            url: "myUrl.php",
            type: "GET",
            success: function ( R )
            {
                $( "#IDselector" ).html( R );
            },
            beforeSend: function ()
            {
                $( "#IDselector" ).html( "Loading" );
            }
        } )
    } );
</script>

In this, your myUrl.php file would contain the logic and printing for the reading of the file and the AJAX would handle what to do when complete. (All data in the myUrl.php that you want to see MUST be printed as you would normally for the user to see))

Worth a read into to allow the page to load fully then read in extra data

Also, look into fopen, fread etc. They are useful and better for reading files IMO

Can O' Spam
  • 2,718
  • 4
  • 19
  • 45
  • This answer demonstrates a lack of understanding of OP's problem. OP's problem is that his WAMP server hangs whenever he calls `readfile`. Using AJAX is not going to fix this. – Dan Oct 12 '15 at 09:36
  • @ARedHerring, I have used this function with WAMP when I came across the same thing, it worked for me so I shared what solved my issue, if you consider this "a lack of understanding", can you please expand on this/edit my answer or some other form of allowing me to improve this for the OP? – Can O' Spam Oct 12 '15 at 10:26
  • Sure. OP's problem is that WAMP hangs due to loading a file into memory; this is presumably due to the file being very large. Using ajax is not going to solve this issue because OP will still be doing the same thing, it will just mean that you now get the response from the browser asynchronously rather than as part of the main page load. Utilising AJAX is not going to change the underlying issue of OP trying to load a very large file into memory instead of read it line-by-line. – Dan Oct 12 '15 at 10:28
  • @ARedHerring, thank you for that, I understand what it is you are saying now, as for my answer: I was simply sharing what had worked for me to solve the same issue that the OP has come across. I am sorry that this is not what the OP may be looking for, but as I say, just an experience that worked for me :) – Can O' Spam Oct 12 '15 at 10:31