Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 10 of 10
  1. #1
    Senior Coder
    Join Date
    Apr 2003
    Location
    Canada
    Posts
    1,063
    Thanks
    2
    Thanked 0 Times in 0 Posts

    problem with append mode when handling a file...

    Hello,
    I am using fopen("chat.txt","a").
    at php.net, they say that if the file doesn't exist, it will attempt to create it.
    Well, when I try it out with a file that doesn't exist, I get the following message:
    Warning: main(chat.txt): failed to open stream: No such file or directory in C:\Program Files\Abyss Web Server\htdocs\chat1.php on line 29

    Fatal error: main(): Failed opening required 'chat.txt' (include_path='.;c:\php4\pear') in C:\Program Files\Abyss Web Server\htdocs\chat1.php on line 29

    What am I doing wrong?
    here is the complete code:
    PHP Code:
    <!DOCTYPE html
    PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" 
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" >
    <head>

    <meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
    <meta name="author" content="Shawn Inder" />

    <title>chat</title>

    </head>
    <body>

    <?php
     
    if(isset($_POST["sender"])){
      echo(
    "
    <div>
    <ul>
    "
    );
     if(isset(
    $_POST["message"])){
      
    $fop fopen("chat.txt","a");
      
    $String "<li>".$_POST["sender"].": ".$_POST["message"]."<li>\n";
      
    flock($fop,LOCK_EX);
      
    fputs($fop,$String);
      
    fclose($fop);
     }
     require(
    "chat.txt");
     echo(
    "
    </ul>
    <form action='"
    .$_SERVER["PHP_SELF"]."' method='post'>
    <input type='hidden' name='sender' value='"
    .$_POST["sender"]."' />
    <input type='text' name='message' />
    <input type='submit' value='Send' />
    </form>
    </div>
      "
    );
     }
     else{
      echo(
    "
    <form action='"
    .$_SERVER["PHP_SELF"]."' method='post'>
    <input type='text' name='sender' />
    <input type='submit' value='Join the chat' />
    </form>
      "
    );
     }
    ?>
    </body>
    </html>
    by the way, if you see anything stupid in my coding, please do tell me as I am new to PHP and havn't a clue on how to do a chat...
    note: I don't expect it to add like a chat program just yet, it's far from done, but I have a question for the next step..
    How do I make "chat.txt" be erased when there are no users left... or whatever a chat does...

    Thanks for any help
    Shawn

  • #2
    Senior Coder missing-score's Avatar
    Join Date
    Jan 2003
    Location
    UK
    Posts
    2,194
    Thanks
    0
    Thanked 0 Times in 0 Posts
    it could be a couple of things (at a quick glance)

    1. $_POST['message'] is not set, so the file is not created when you require it (the errors you have happen when a required file is not found)

    2. You call flock($fop, LOCK_EX) but you dont unlock it after writing using flock($fop, LOCK_UN). I dont know if this would matter or not.

  • #3
    Senior Coder
    Join Date
    Feb 2004
    Posts
    1,206
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Well you're using require() and if the file isn't there, then you're pretty much hooped.

    I would use fopen() to try opening the file and spit out its contents.

    As for knowing when a user has left, you'd have to have that information someplace, like a file or database entry. If you used a file, you can just overwrite it each time and either increment the number (when a user signs on) or decrement the number (when a user signs off).

    As for a database, you can set an attribute in the user's record to say if he/she is online or offline. A simple query could count the number of users online -- if that value is 0, then you can erase the file.

    You might also scrapping the file idea altogether and store everything in the database... just a thought.

    Hope that helps some,
    Sadiq.

  • #4
    Senior Coder
    Join Date
    Apr 2003
    Location
    Canada
    Posts
    1,063
    Thanks
    2
    Thanked 0 Times in 0 Posts
    missing-score, you're right about flock($fop,LOCK_UN); I had missed that.
    but for # one, if you look at my code, it calls all that stuff only if $_POST["sender"] has been set, and it is set at the same time as $_POST["message"], so there is no way that could be the problem...



    sad
    Well you're using require() and if the file isn't there, then you're pretty much hooped.
    Quote Originally Posted by php.net
    Open for writing only; place the file pointer at the end of the file. If the file does not exist, attempt to create it.
    what more to say...

    I would use fopen() to try opening the file and spit out its contents.
    I don't know how to "split"... yet (nor do I know what it means)

    And for the user thing, I realised that was far from being the best way to do it, because anyways, if one user inputs some text, the second user has to reload the page to see the update...
    So I probably on the wrong track here.
    Anyone know how to make a chat program (using php of course)?


    Thanks for your help though
    Shawn

  • #5
    Senior Coder missing-score's Avatar
    Join Date
    Jan 2003
    Location
    UK
    Posts
    2,194
    Thanks
    0
    Thanked 0 Times in 0 Posts
    fopen and require are different things.... If a required file does not exist PHP will throw an error, not attempt to create it. Im off to school now, ill try give you some more help when I get back.

  • #6
    Senior Coder
    Join Date
    Feb 2004
    Posts
    1,206
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I said spit, not spLit. In other words, echo the contents of the file, line by line.

    I found a few links that are open source chat programs. Maybe they'll give you some help/direction:
    http://www.hotscripts.com/PHP/Script.../Chat_Scripts/
    http://www.scriptz.com/PHP/Chat_Scripts/
    http://www.php-chat.org/
    http://www.webdev.ro/products/webchat/

    And there are many more online. Just google php chat and you'll find plenty of more links (if these weren't enough..)

    Good luck,
    Sadiq.

  • #7
    Senior Coder
    Join Date
    Apr 2003
    Location
    Canada
    Posts
    1,063
    Thanks
    2
    Thanked 0 Times in 0 Posts
    missing-score, in my script, the require is only called if $_POST["sender"] is set, and whenever you set $_POST["sender"], it also set $_POST["message"] which means that the part of code using fopen WILL be called. since the file doesn't exist, fopen WILL create, and THEN, require will be caled (on an existing file). Unless I am mistaken..

    sad69: I am not familiar with either spit or split, don't know what they do or how to use 'em...
    Thanks for the links, I guess I'll get my inspiration from there (just thought I'd try it by myself first)


    Thanks for all your help everyone
    If I have trouble, I'll just post back in this thread
    Shawn

  • #8
    Super Moderator
    Join Date
    May 2002
    Location
    Perth Australia
    Posts
    4,047
    Thanks
    10
    Thanked 93 Times in 91 Posts
    but the chat file never gets created as the $_REQUEST['message']; is never set in the first place ... moving the require as suggested removes the initial problem

    PHP Code:
    <?
     
    if(isset($_POST["message"])){ 
      
    $fop fopen("chat.txt","a"); 
      
    $String "<li>".$_POST["sender"].": ".$_POST["message"]."<li>\n"
      
    flock($fop,LOCK_EX); 
      
    fputs($fop,$String); 
      
    fclose($fop); 
      require(
    "chat.txt"); 
     } 
    ?>
    but that will cause other issues

    PHP Code:
    <?
     
    if(isset($_POST["message"])){ 
      
    $fop fopen("chat.txt","a"); 
      
    $String "<li>".$_POST["sender"].": ".$_POST["message"]."<li>\n"
      
    flock($fop,LOCK_EX); 
      
    fputs($fop,$String); 
      
    fclose($fop); 
     } 
    @include_once(
    'chat.txt');
    ?>
    would solve both issues , though I think you ought to @include the file regardless (e.g. outside the if/else blocks)
    resistance is...

    MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)

  • #9
    Senior Coder missing-score's Avatar
    Join Date
    Jan 2003
    Location
    UK
    Posts
    2,194
    Thanks
    0
    Thanked 0 Times in 0 Posts
    you could also make use of the file_exists function ( http://www.php.net/file-exists )

  • #10
    Senior Coder
    Join Date
    Apr 2003
    Location
    Canada
    Posts
    1,063
    Thanks
    2
    Thanked 0 Times in 0 Posts
    I don't know about @include_once, I'll go check it out.

    missing_score: thanks for the suggestion, I'll try to work something out with what I have.
    Shawn


  •  

    Posting Permissions

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