Referencing a txt file

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

    Referencing a txt file

    Hi,

    On receiving input from a user I need to reference it to a text file to see
    if that entry is present in the file. Is it possible to do this in php, or
    will it need to be done in Javascript? Or is it not possible at all? Each
    entry in the text file is on a new line if that makes any difference.

    Cheers,

    Paul


  • Erwin Moller

    #2
    Re: Referencing a txt file

    Paul Morrison wrote:
    [color=blue]
    > Hi,
    >[/color]

    Hi
    [color=blue]
    > On receiving input from a user I need to reference it to a text file to
    > see if that entry is present in the file. Is it possible to do this in
    > php, or will it need to be done in Javascript?[/color]

    How do you expect Javascript to open a file?
    Javascript is crippled for very good reasons when it comes to reading local
    files.

    Or is it not possible at[color=blue]
    > all?[/color]

    Yes, it is easy in php.
    Try something like this:

    On receiving the userinput in php:
    // assuming the filed is named 'username'
    $passedusername = $_POST["username"];

    // open your file:
    $filecontentes = file('/home/usr/paul/myfile.txt');

    // assuming your file contained usernames
    if (in_array($pass edusername,$fil econtents)){
    // found it
    } else {
    / not found it
    }



    Each entry in the text file is on a new line if that makes any[color=blue]
    > difference.[/color]

    of course that makes a difference. :P
    If you just search the file for the username (substringsearc h) you will get
    matches for username A while in your file is Andre.
    You don't want that, so match them per line.


    Go to www.php.net for details on the functions.

    Regards,
    Erwin Moller
    [color=blue]
    >
    > Cheers,
    >
    > Paul[/color]

    Comment

    • Michael Austin

      #3
      Re: Referencing a txt file

      Paul Morrison wrote:
      [color=blue]
      > Hi,
      >
      > On receiving input from a user I need to reference it to a text file to see
      > if that entry is present in the file. Is it possible to do this in php, or
      > will it need to be done in Javascript? Or is it not possible at all? Each
      > entry in the text file is on a new line if that makes any difference.
      >
      > Cheers,
      >
      > Paul
      >
      >[/color]

      Using that method typically will not scale - especially if you are expecting new
      entries in the file AND reading the file concurrently. You may want to consider
      using a database (Oracle, SQLServer,MySQL ).

      There are various ways to do what you want, just remember, the larger the file
      the longer it will take each "hit" to process (which is why you design a
      database with proper indexing and a whole lot more)

      read the entire file into an array and process through the array until 1) you
      get to the end (no records found) or 2)you locate the requested record.
      Depending on file size, this may be all you need.

      you can use a system call to "grep" or "find" the requested entry - more
      difficult to code, but can be faster if the file is very large.

      When doing these sorts of things, just remember reading a 1MB file into an array
      takes ~1MB+ of physical memory just for the one file. And depending on the
      number of page hits, you could potentially be running a memory starved system
      which equals poor performance - Use a database engine.

      --
      Michael Austin.
      DBA Consultant
      Donations welcomed. Http://www.firstdbasource.com/donations.html
      :)

      Comment

      Working...