Using fopen

Discussion in 'PHP' started by pc_user, Jan 1, 2007.

  1. #1
    Using PHP and fopen, I can open a file, even run while statements, however, on larger files, the scripts look stalled.

    How do I pull a file and print out line by line or at least in chunks so that the user sees the output as it's coming through?
     
    pc_user, Jan 1, 2007 IP
  2. tandac

    tandac Active Member

    Messages:
    337
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    58
    #2
    tandac, Jan 1, 2007 IP
  3. INTEMPO

    INTEMPO Active Member

    Messages:
    64
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    91
    #3
    You may use ob_start(); and ob_end_flush(); functions to print out whole text after reading it from the file. You ma read it line by line print it out into the buffer in some variable:

    echo $newLineFromFile;
    PHP:
    after running ob_start(); function. Buffer creates automatically after this function was run.

    After the whole file was printed out into the buffer run the ob_end_flush(); function and all buffer will be printed out into the browser.
     
    INTEMPO, Jan 2, 2007 IP
  4. krakjoe

    krakjoe Well-Known Member

    Messages:
    1,795
    Likes Received:
    141
    Best Answers:
    0
    Trophy Points:
    135
    #4
    if you use fgets() to retrieve data and only pass the resource name to it, it will return data line by line ( \n ), as others have suggested on larger files clearing of the buffer / cache is necessary, also set_time_limit(0); is a good idea.

    If you're a little more specific about what you want to do with the data line by line we can probably be of more constructive help.
     
    krakjoe, Jan 2, 2007 IP