Fwrite gives an unlogical error

Discussion in 'PHP' started by Pieman, May 2, 2008.

  1. #1
    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.

    My script:
    <?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 asshole!</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 asshole!");
    			} 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);
    			stripslashes($header2);
    			
    			if($handle = fopen("header.php", "w") && $handle2 = fopen("admin/header.php", "w")) {
    
    				fwrite($handle, $header);
    				fclose($handle);
    																
    				fwrite($handle2, $header2);
    				fclose($handle2);
    								
    				echo "<br/>$handle";
    				echo "<br/>$handle2";
    				
    			} 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'] = 1;
    
    		echo "Successfully connected to $host.<br/><br/>";
    		}
    	}
    } else if($_SESSION['step'] == 2) {}
    ?>
    </body>
    </html>
    
    PHP:
    The error I get is:

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

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

    In an attempt to solve the problem, I echoed $handle and $handle2,
    $handle outputted "1"
    $handle2 outputted "Resource id #3"

    I still have no idea why they give two different outputs though.

    Any suggestions?

    P.S line 120 and 121 are these two lines:
    fwrite($handle, $header);
    fclose($handle);
    In case you hadn't noticed.
     
    Pieman, May 2, 2008 IP
  2. youscript

    youscript Peon

    Messages:
    12
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    You can use is_writable($filename) to check the is writeable first.
     
    youscript, May 2, 2008 IP
  3. Pieman

    Pieman Peon

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    is_writable returned true...
     
    Pieman, May 2, 2008 IP
  4. Barti1987

    Barti1987 Well-Known Member

    Messages:
    2,703
    Likes Received:
    115
    Best Answers:
    0
    Trophy Points:
    185
    #4
    Change this:

    
    if($handle = fopen("header.php", "w") && $handle2 = fopen("admin/header.php", "w")) {
    
                    fwrite($handle, $header);
                    fclose($handle);
                                                                    
                    fwrite($handle2, $header2);
                    fclose($handle2);
                                    
                    echo "<br/>$handle";
                    echo "<br/>$handle2";
                    
                } 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.");
                            
                }
    
    PHP:
    To This:
    
                    $handle = fopen("header.php", "w") or die('Unable to open header file.');
                    $handle2 = fopen("admin/header.php", "w") or die ('Unable to open admin header file.');
    
                    fwrite($handle, $header);
                    fclose($handle);
                                                                    
                    fwrite($handle2, $header2);
                    fclose($handle2);
                                    
                    echo "<br/>$handle";
                    echo "<br/>$handle2";
    
    PHP:
    It's the same, but easier to debug.

    Peace,
     
    Barti1987, May 2, 2008 IP