trouble reading from file

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • somaboy mx

    #1

    trouble reading from file

    hi,

    I'm on winXPpro / apache 1.2 / php 4.4.x

    I'm experimenting with writing and reading from textfiles via php. I can
    create a file with fopen, write to it, but I'm having trouble reading
    from the file.

    here's a snippet:

    if (!$keywfile = file("keyw.txt" ) || !$keywfile = file("desc.txt" ))
    {
    $error = "couldn't open files<br>";

    } else {

    foreach($keywfi le as $line)
    {
    $keywords .= $line;
    }
    foreach($keywfi le as $line)
    {
    $desc .= $line;
    }

    }

    I get the following error: Warning: "Invalid argument supplied for
    foreach() in d:\filesystem\t mp5o3n6eulkr.ph p on line 25"

    I checked the file handles with print_r and they both output "1". isn't
    the file() function supposed to return an array?I have verified that the
    files are not empty.


    ..soma
  • Micha³ Wo¼niak

    #2
    Re: trouble reading from file

    One quick glance of an experienced eye allowed to understand the blurred
    and almost unreadable somaboy mx's handwriting:

    [snip][color=blue]
    > I get the following error: Warning: "Invalid argument supplied for
    > foreach() in d:\filesystem\t mp5o3n6eulkr.ph p on line 25"
    >
    > I checked the file handles with print_r and they both output "1". isn't
    > the file() function supposed to return an array?I have verified that
    > the files are not empty.[/color]

    Nope, read the manual at www.php.net
    You have to use fgets() to get lines from file.

    Cheers
    Mike

    Comment

    • somaboy mx

      #3
      Re: trouble reading from file

      Michał Woźniak wrote:[color=blue]
      > One quick glance of an experienced eye allowed to understand the blurred
      > and almost unreadable somaboy mx's handwriting:
      >
      > [snip]
      >[color=green]
      >>I get the following error: Warning: "Invalid argument supplied for
      >>foreach() in d:\filesystem\t mp5o3n6eulkr.ph p on line 25"
      >>
      >>I checked the file handles with print_r and they both output "1". isn't
      >>the file() function supposed to return an array?I have verified that
      >>the files are not empty.[/color]
      >
      >
      > Nope, read the manual at www.php.net
      > You have to use fgets() to get lines from file.
      >
      > Cheers
      > Mike[/color]

      Thanks Mike, fgets() does the job...

      ..soma

      Comment

      • BKDotCom

        #4
        Re: trouble reading from file

        ???
        Yes, file() will read the entire file into an array
        there are a lot more options for reading a file than just fopen and
        fgets()

        a problem I see is that you're using the same variable ($keywfile) for
        both:
        if (!$keywfile = file("keyw.txt" ) || !$keywfile = file("desc.txt" ))

        and try this format
        if ( false == $keywfile = file('keyw.txt' ) )
        {
        // couldn't open
        }
        or

        if ( $keywfile = file('keyw.txt' ) )
        {
        $keywords = '';
        foreach($keywfi le as $line)
        {
        $keywords .= $line;
        }
        // or instead of the foreach, just use
        // $keywords = implode('',$key wfile);
        }

        Comment

        • somaboy mx

          #5
          Re: trouble reading from file

          BKDotCom wrote:[color=blue]
          > ???
          > Yes, file() will read the entire file into an array
          > there are a lot more options for reading a file than just fopen and
          > fgets()
          >
          > a problem I see is that you're using the same variable ($keywfile) for
          > both:
          > if (!$keywfile = file("keyw.txt" ) || !$keywfile = file("desc.txt" ))
          >
          > and try this format
          > if ( false == $keywfile = file('keyw.txt' ) )
          > {
          > // couldn't open
          > }
          > or
          >
          > if ( $keywfile = file('keyw.txt' ) )
          > {
          > $keywords = '';
          > foreach($keywfi le as $line)
          > {
          > $keywords .= $line;
          > }
          > // or instead of the foreach, just use
          > // $keywords = implode('',$key wfile);
          > }
          >[/color]
          yes, that is some awkward code :( , I noticed and fixed it right after I
          posted. But using fgets in combination with feof() fixed the problem of
          not being able to iterate over the file contents.

          thx,

          ..soma

          Comment

          Working...