Desperately simple upload error

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
mesz
Forum Contributor
Posts: 216
Joined: Fri May 23, 2003 8:11 am
Location: M/cr

Desperately simple upload error

Post by mesz »

Dear All,
Please help.
When I started learning this PHP malarkey it was to build a news update system ( posh term for blog, I know ).
Anyway this took so long and was so painful that when I finally got it to work I was so happy that I accepted it as finished despite errors.
This was OK because the public would not see it...however they now will so I must ask where the hell is the error in this script...it works and I just cannot spot the error.
The upload script:

Code: Select all

<?
if($HTTP_POST_VARS['submit']) 
        $fp = fopen('news.txt','a');
        $line = date("m.d.y") . "|" . $HTTP_POST_VARS['name'];
        $line .= "|" . $HTTP_POST_VARS['news'];
        $line = str_replace("\r\n","<BR>",$line);
        $line .= "\r\n";
        fwrite($fp, $line);
        if(!fclose($fp))
?>
<FORM ACTION="<?=$PHP_SELF?>" METHOD="POST"NAME="newsentry">
Update added by:
<INPUT TYPE="text" SIZE="30" NAME="name">
<TEXTAREA NAME="news" COLS="50"ROWS="20"></TEXTAREA>
<INPUT TYPE="reset" NAME="reset" VALUE="Start again">
<INPUT TYPE="submit" NAME="submit" VALUE="Post">
</FORM>
The script that reads the upload:

Code: Select all

<?PHP
$data = file('news.txt');
$data = array_reverse($data);
foreach($data as $element) {
    $element = trim($element);
    $pieces = explode("|", $element);
    echo $pieces[2] . "<BR>" . "<b>Posted by " . $pieces[1] . " on " . $pieces[0] . "</b><BR><BR>";
}
?>
The errors received upon opening upload page:
Warning: fwrite(): supplied argument is not a valid stream resource in /home/.sites/137/site468/web/pstore/addnews.php on line 19

Warning: fclose(): supplied argument is not a valid stream resource in /home/.sites/137/site468/web/pstore/addnews.php on line 20

Cheers for any help you can afford me - this is casuing me a real headache now.
Tubbietoeter
Forum Contributor
Posts: 149
Joined: Fri Mar 14, 2003 2:41 am
Location: Germany

Post by Tubbietoeter »

First of all, what does this do: "if(!fclose($fp))" ... there are no "{" or anything, that belongs to that if.

then the error means, that when calling fwrite, you didn't give the script a valid file handle, which implies, that it couln't be opened.

try:
$fp = fopen('news.txt','a');
if (!$fp) {
echo "Sorry could'n open file!<br>";
} else {
// the rest
}


When opening the file, you give the modus "a" (append); either the file does not exist or there are not enough rights to create the file or append to it. check on that.
User avatar
mesz
Forum Contributor
Posts: 216
Joined: Fri May 23, 2003 8:11 am
Location: M/cr

Post by mesz »

This worked to perfection!
Cheers. :P
If this was expertsexchange I would award you 500 points.
Cheers Mukka.
Tubbietoeter
Forum Contributor
Posts: 149
Joined: Fri Mar 14, 2003 2:41 am
Location: Germany

Post by Tubbietoeter »

No problemo ... :wink: :lol:
Post Reply