Jump to content

"fopen" function


wattsup88

Recommended Posts

Hey guys! I cant get the fopen function to work in this block of code... Any idea why? or any alternative ideas... Just trying to create a newsfeed....

 

<?php

//this should all go into one file. I would name it addnews.php
if($HTTP_POST_VARS['submit']) {
    if($HTTP_POST_VARS['password'] == 'pass') {
        if(!$HTTP_POST_VARS['name']) {
            echo "You must enter a name";
            exit;
        }
        if(!$HTTP_POST_VARS['news']) {
            echo "You must enter some news";
            exit;
        }
        if(strstr($HTTP_POST_VARS['name'],"|")) {
            echo "Name cannot contain the pipe symbol - |";
            exit;
        }
        if(strstr($HTTP_POST_VARS['news'],"|")) {
            echo "News cannot contain the pipe symbol - |";
            exit;
        }
        $fp = fopen('news.txt','a');
        if(!$fp) {
            echo "Error opening file!";
            exit;
        }
        $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)) {
            echo "Error closing file!";
            exit;
        }        
    } else {
        echo "Bad Password";
    }
}

?>

Link to comment
https://forums.phpfreaks.com/topic/79031-fopen-function/
Share on other sites

By "work" i mean that this block always returns true and therefore echos the message and exits. I want this block to return false (see code above).

 

if(!$fp) {
            echo "Error opening file!";
            exit;
        }

 

Also i believe the file is writable but how do i tell... I right-clicked and check and the "read-only" box is not checked if thats what you mean... oh and the folder on my server is writable of course...

 

and yes i know about the $_POST thing i copied and pasted some old code and i havent replaced it... Thanks for bringing that to my attention though  :)

 

 

Sorry to be so noobish but i have never dealt with this function before...

Thanks for the help already and

 

Link to comment
https://forums.phpfreaks.com/topic/79031-fopen-function/#findComment-402263
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.