CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Aug 2002
    Location
    Brazil
    Posts
    730

    PHP Text Hit Counter: fwrite(): supplied argument is not a valid stream resource

    I'm newbie in PHP and I am coding a simple text counter. I'm using PHP version 4.3.4 in Xitami 2.5b4 and WinXP Pro.

    When I execute the php file, this is the output:
    Code:
    ./kounterfiles/~hitcounter~index.php.txt
    Warning: fwrite(): supplied argument is not a valid stream resource in D:\WebDevelop\local\hitcounter\index.php on line 29
    
    Warning: fclose(): supplied argument is not a valid stream resource in D:\WebDevelop\local\hitcounter\index.php on line 30
    
    Hit Counter: Error opening file for reading.
    Note: The "Error opening file for reading" is an error handler that I made due to the fact that it will overload ~350mb of RAM mem if it is not there. And also, it seems to be hiding the same error shown above it ("supplied argument is not a valid stream resource")...

    Here is the code:

    Code:
    <?php
    
    /*
    	Based on php.inc.ru's counter
    	Dev by BubuX ([email protected])
    */
    
    // Config settings here
    $file_dir = "./kounterfiles/";
    $file_ext = ".txt";
    $currfile = $file_dir . str_replace('/','~',$_SERVER['SCRIPT_NAME']) . $file_ext;
    echo $currfile;
    // Log visit
    function log_visit() {
    	$fp = fopen($currfile, "a");
    	//if (!$fp) {
    		//echo "<br>Hit Counter: Error opening file for appending.<br>";
    		//}
    	//else {
    		$today = getdate();
    		$month = $today[month];
    		$mday = $today[mday];
    		$year = $today[year];
    		$date = $mday . $month . $year;
    		$line = $REMOTE_ADDR . "|" . $mday . $month . $year . "\13\10";
    		fwrite($fp, $line);
    		fclose($fp);
    	//}
    }
    
    // Show visit count
    function show_visit() {
    	$fp = fopen ($currfile, "r");
    	if (!$fp) {
    		echo "<br>Hit Counter: Error opening file for reading.<br>";
    		}
    	else {
    		$visits = 0;
    		while (!feof ($fp)) {
    			$line = fgets($fp, 4096);
    			$visits++;
    			//echo $buffer;
    		}
    		fclose ($fp);
    	
    		// Display visits count
    		echo $visits;
    	}
    }
    
    log_visit();
    show_visit();
    ?>
    All consequences are eternal in some way.

  2. #2
    Join Date
    Aug 2002
    Location
    Brazil
    Posts
    730
    Ahh just found the answer!!!!
    I didn't knowed about having to use the global statement for acessing variables outside a block... Like:

    PHP Code:
    $outside_var "God love you!";  // Declare a variable

    function Glory() {
      echo 
    $outside_var "<br>";                                 // $outside_var = "" (inside this function)
      
    global $outside_var// add reference to the oustide variable
                                     // Now $outside_var = "God love you!" (inside this function)

      
    echo $outside_var "<br>";  //Test

    So now I recognize that the filename passed to fopn() was a "". Now the hit counter code stays like that:
    PHP Code:
    <?php

    /*
        Based on php.inc.ru's counter
        Dev by BubuX ([email protected])
    */

    // Config settings here
    $file_dir "./kounterfiles/";
    $file_ext ".txt";
    $currfile $file_dir str_replace('/','~',$_SERVER['SCRIPT_NAME']) . $file_ext;

    // Log visit
    function log_visit() {
        global 
    $currfile;
        
    $fp fopen($currfile"a");
        
    //if (!$fp) {
            //echo "<br>Hit Counter: Error opening file for appending.<br>";
            //}
        //else {
            
    $today getdate();
            
    $month $today[month];
            
    $mday $today[mday];
            
    $year $today[year];
            
    $date $mday $month $year;
            
    $line $_SERVER['REMOTE_ADDR'] . "|" $mday $month $year "\13\10";
            
    fwrite($fp$line);
            
    fclose($fp);
        
    //}
    }

    // Show visit count
    function show_visit() {
        global 
    $currfile;
        
    $fp fopen ($currfile"r");
        if (!
    $fp) {
            echo 
    "<br>Hit Counter: Error opening file for reading.<br>";
            }
        else {
            
    $visits 0;
            while (!
    feof ($fp)) {
                
    $line fgets($fp4096);
                
    $visits++;
                
    //echo $buffer;
            
    }
            
    fclose ($fp);
        
            
    // Display visits count
            
    echo $visits;
        }
    }

    log_visit();
    show_visit();
    ?>
    Last edited by bubu; January 19th, 2004 at 02:13 PM.
    All consequences are eternal in some way.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured