SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Member
    Join Date
    Dec 2003
    Location
    new york
    Posts
    11
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Getting parse error on trying to read from a text file

    Sorry for this newbie question but I hope someone can tell me why I'm getting a parse error on the script. Specifically I'm getting the parse error on the following line:

    $myFile = fopen($filename, "r");


    Here's My Code:

    <?php

    $filename ="file.txt";****

    $myFile = fopen($filename, "r");
    if(! $myFile){************

    print ("File could not be opened.");

    exit;
    }

    $fcontents = file($filename);

    **$limit = 5;

    for ($i = 0; $i < $limit; $i++){

    ****$line = $fcontents[$i];**

    ******if($line != ""){**
    ********echo "$line \n";
    **********}
    **********
    *****}
    *****
    fclose($myFile);

    ?>

  2. #2
    SitePoint Member
    Join Date
    Dec 2003
    Location
    new york
    Posts
    11
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Sorry about the mess...

    The code in question can be viewed at the original source here:

    http://www.onaje.com/php/code/readfile2.phps

  3. #3
    SitePoint Enthusiast DmS's Avatar
    Join Date
    Jan 2004
    Location
    Stockholm, Sweden
    Posts
    94
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Don't know if you fixed it or not but I copied your code from the link and ran it locally and it worked w/o problems, The only thing I changed was the filename to a file that I knew I had.
    /Dan
    { knowledge is what remains once you forget what you learned }
    Home: DmSProject Tutorials: GurusNetwork
    Committed at:
    OzoneAsylum + Blog

  4. #4
    SitePoint Wizard Dangermouse's Avatar
    Join Date
    Oct 2003
    Posts
    1,024
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Are you trying to only read 5 lines of a file into an array?

    The file() function reads a file contents into an array, then it just gets the first 5 items in the array, then you can loop through these ten items and do what you wish

    Untested but i think it should work
    PHP Code:
    $file file("yourfile");
    $lines array_splice($file,5);
    foreach (
    $lines as $linenum => $line) {
    print 
    "$linenum = $line<br>";


  5. #5
    SitePoint Member
    Join Date
    Dec 2003
    Location
    new york
    Posts
    11
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by DmS
    Don't know if you fixed it or not but I copied your code from the link and ran it locally and it worked w/o problems, The only thing I changed was the filename to a file that I knew I had.
    /Dan
    DmS -
    Thanks. Yes, this is what I don't understand. I don't think anything is wrong with the script and it should work but it's not on my local machine. Do you happen to know if there are certain lines in my php.ini that could potentially cause this error?

  6. #6
    SitePoint Member
    Join Date
    Dec 2003
    Location
    new york
    Posts
    11
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Dangermouse
    Are you trying to only read 5 lines of a file into an array?

    The file() function reads a file contents into an array, then it just gets the first 5 items in the array, then you can loop through these ten items and do what you wish

    Untested but i think it should work
    PHP Code:
    $file file("yourfile");
    $lines array_splice($file,5);
    foreach (
    $lines as $linenum => $line) {
    print 
    "$linenum = $line<br>";

    Dangermouse-
    Thanks for your help. Your script does the same thing I was trying to do and it works. Strange why the original script kept giving me the parse error. BTW, is there a way to print out the first 5 lines of the text file rather than the last 5? Thanks in advance.

  7. #7
    SitePoint Member
    Join Date
    Dec 2003
    Location
    new york
    Posts
    11
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by soxiam
    Dangermouse-
    Thanks for your help. Your script does the same thing I was trying to do and it works. Strange why the original script kept giving me the parse error. BTW, is there a way to print out the first 5 lines of the text file rather than the last 5? Thanks in advance.
    Answered my own question from the PHP manual:
    $lines = array_splice($file, 0, 5);


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •