Click to See Complete Forum and Search --> : PHP Text Hit Counter: fwrite(): supplied argument is not a valid stream resource


bubu
January 19th, 2004, 12:04 PM
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:./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:

<?php

/*
Based on php.inc.ru's counter
Dev by BubuX (BubuX@phreaker.net)
*/

// 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();
?>

bubu
January 19th, 2004, 12:46 PM
Ahh just found the answer!!!!
I didn't knowed about having to use the global statement for acessing variables outside a block... Like:


$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

/*
Based on php.inc.ru's counter
Dev by BubuX (BubuX@phreaker.net)
*/

// 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($fp, 4096);
$visits++;
//echo $buffer;
}
fclose ($fp);

// Display visits count
echo $visits;
}
}

log_visit();
show_visit();
?>