Read a file of string into what?

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

    #1

    Read a file of string into what?

    Pardon the simple question, but I have just begun to learn PHP. So far so
    good - all the examples in my books actually work.

    One thing that none of them address is how to read a file of strings (like
    names or URLs or whatever) into something so that they can be deleted,
    modified or a new one added. In a language like Perl/TK or Delphi a
    listbox is used if you want to edit line by line or a memo box if you want
    to edit like you are in Kwrite.

    All the examples assume that I want to enter data into a Text area or
    Listbox by keyboard, then read it back in. Never the other way around. I
    can use a Fget routine and read the file to the screen, but not to
    anything in which I can edit yet.

    So far I have learned a ton of stuff trying, but haven't made it work yet.

    Should I be reading the data into a Textarea, a Listbox or something else?

    Anybody got a short example.

    Thanks
    Konan
  • Botan Guner

    #2
    Re: Read a file of string into what?

    Have a look at php manuel. You can download it from php.net. But i
    think this will work for you.

    $handle = fopen("http://www.example.com/", "rb");
    $contents = '';
    while (!feof($handle) ) {
    $contents .= fread($handle, 8192);
    }
    fclose($handle) ;

    <input type="text" value="<?php echo $contents; ?>">

    Comment

    • Michael Vilain

      #3
      Re: Read a file of string into what?

      In article <pan.2005.06.05 .02.34.13.96028 1@nowhere.com>,
      Konan <knnanxxxx@nowh ere.com> wrote:
      [color=blue]
      > Pardon the simple question, but I have just begun to learn PHP. So far so
      > good - all the examples in my books actually work.
      >
      > One thing that none of them address is how to read a file of strings (like
      > names or URLs or whatever) into something so that they can be deleted,
      > modified or a new one added. In a language like Perl/TK or Delphi a
      > listbox is used if you want to edit line by line or a memo box if you want
      > to edit like you are in Kwrite.
      >
      > All the examples assume that I want to enter data into a Text area or
      > Listbox by keyboard, then read it back in. Never the other way around. I
      > can use a Fget routine and read the file to the screen, but not to
      > anything in which I can edit yet.
      >
      > So far I have learned a ton of stuff trying, but haven't made it work yet.
      >
      > Should I be reading the data into a Textarea, a Listbox or something else?
      >
      > Anybody got a short example.
      >
      > Thanks
      > Konan[/color]

      You have a file consisting of lines of text. fgets(HANDLE) will return
      a line at a time into a variable. So you use it within a loop and store
      into an array. Now you have an array who's elements contain the lines
      of your file. You can do whatever you want to the that array with the
      various array manipulation functions and output whatever you wish in
      HTML.

      What's the problem you're trying to solve? I'm confused here.

      --
      DeeDee, don't press that button! DeeDee! NO! Dee...



      Comment

      • Dale

        #4
        Re: Read a file of string into what?

        I think you mean this:
        $handle = fopen("filename ","rt"); // where filename is a file on the
        server
        $contents = "";
        while (!feof($handle) )
        $contents .= fgets($handle);
        fclose($handle)
        ?>
        <textarea name="textinput form" cols="30" rows="5"><?php echo $contents;
        ?></textarea>

        assuming you know all about forms.

        Comment

        Working...