Help - Search - Members - Calendar
Full Version: Problem with fwrite
HTMLHelp Forums > Programming > Server-side Scripting
Pieman
So I'm writing an install script for my blog software, however there seems to be an error in my script and I just can't find it.

CODE
<? session_start(); ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>

<body>
<?php
//Check to see if step has already been done
if($_SESSION['step'] == 1 || !isset($_SESSION['step'])) {

    if(!isset($_POST['submit'])) {
    ?>
    <form action="<? $_SERVER['PHP_SELF']; ?>" method="post">
    Host <input type="text" name="host" />
    <br/>
    Username <input type="text" name="user" />
    <br/>
    Password <input type="password" name="pass"  />
    <br/>
    Database <input type="text" name="db"  />
    <input type="submit" name="submit" value="Submit" />
    </form>
    <?php
    
    } else {
    
        $host = $_POST['host'];
        $user = $_POST['user'];
        $pass = $_POST['pass'];
        $db = $_POST['db'];
    
        if(empty($host) || empty($user) || empty($pass) || empty($_POST['db'])) {
    
            unset($_POST['submit']);
            die("Please fill in all the fields.");
        }
    
        @mysql_connect($host, $user, $pass) or die ("Could not connect, please check your mysql information.");
    
        @mysql_select_db($_POST['db']) or die ("Invalid database specified");
        
        if(mysql_connect($host, $user, $pass) && mysql_select_db($_POST['db'])) {
        
            $header = '
            <?php
            session_start();
            ob_start();
                        
            $host = "'.$host.'";
            $user = "'.$user.'";
            $pass = "'.$pass.'";
            $db = "'.$db.'";
                            
            $connect = mysql_connect($host, $user, $pass) or die (mysql_error());
                        
            mysql_select_db($db);
                        
            $ip = $_SERVER[\'REMOTE_ADDR\'];
            $query2 = "SELECT ip FROM banned WHERE ip =\'$ip\'";
                        
            $result2 = mysql_query($query2) or die (mysql_error());
                        
            if(mysql_num_rows($result2) > 0) {
                        
                die ("<center>You\'ve been banned *beep*!</center>");
                            
            }
            ?>
            ';
            
            $header2 = '
            <?php
            session_start();
            ob_start();
            
            $host = "'.$host.'";
            $user = "'.$user.'";
            $pass = "'.$pass.'";
            $db = "'.$db.'";
                
            $connect = mysql_connect($host, $user, $pass);
                
            mysql_select_db($db);
            
            $ip = $_SERVER[\'REMOTE_ADDR\'];
            $query2 = "SELECT * FROM banned WHERE ip =\'$ip\'";
            
            $result2 = mysql_query($query2);
            
            if(mysql_num_rows($result2) > 0) {
                die("You\'ve been banned *beep*!");
            } else {
            
            if (!isset($_SESSION[\'uname\']) && !isset ($_SESSION[\'passw\'])) {
                include("../login.php");
                die();
            } else {
                $uname = $_SESSION[\'uname\'];
                $passw = $_SESSION[\'passw\'];
                
                $query = "SELECT * FROM login WHERE username = \'$uname\' AND password = \'$passw\'";
                
                $result = mysql_query($query);
                
                if (mysql_num_rows($result) < 1) {
                    include("../login.php");
                    die ();
                }else {}
                }
            }
            ?>
            ';
        
            stripslashes($header);
            if($handle = fopen("header.php", "w") && $handle2 = fopen("admin/header.php", "w")) {
                
                fwrite($handle2, $header2);
                fclose($handle2);
                
                fwrite($handle, $header);
                fclose($handle);
                
            } else {
            
                die("A fatal error occured, we recommend aborting the installation and start over from a fresh install. If this error persits consult our troubleshooting.");
                        
            }
    
        $_SESSION['step'] = 2;

        echo "Successfully connected to $host.<br/><br/>";
        }
    }
} else if($_SESSION['step'] == 2) {}
?>
</body>
</html>


The error I get is:

Warning: fwrite(): supplied argument is not a valid stream resource in -edited out- on line 123
Warning: fclose(): supplied argument is not a valid stream resource in -edited out- on line 124

I have no idea why it's giving this error, as it's almost the exact same piece of code as lines 120 and 121. Yet those don't create any errors.

Any suggestions?
Brian Chandler
QUOTE
I have no idea why it's giving this error, as it's almost the exact same piece of code as lines 120 and 121. Yet those don't create any errors. Any suggestions?


I've no idea what the problem is, but it's no good just staring at the code. Print out the values of $handle and $handle2. Are both being returned correctly? (Are both valid stream handles, which I think means positive integers? What is the error return from fopen() ?)
Pieman
Hmm,

printing $handle gives "1", printing $handle2 gives "Resource id #3". I still have no idea what's happening though.
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2010 Invision Power Services, Inc.