...

View Full Version : problem with append mode when handling a file...



shlagish
04-29-2004, 10:52 PM
Hello,
I am using fopen("chat.txt","a").
at php.net (http://ca.php.net/fopen), 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:


<!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 :)

missing-score
04-29-2004, 11:03 PM
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.

sad69
04-29-2004, 11:08 PM
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.

shlagish
04-30-2004, 03:20 AM
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.

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 :)

missing-score
04-30-2004, 07:59 AM
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.

sad69
04-30-2004, 05:22 PM
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/Scripts_and_Programs/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.

shlagish
05-01-2004, 03:39 AM
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

firepages
05-01-2004, 07:25 AM
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



<?
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 ;)



<?
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)

missing-score
05-01-2004, 11:27 AM
you could also make use of the file_exists function ( http://www.php.net/file-exists )

shlagish
05-02-2004, 11:16 PM
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.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum