Script not working, no errors though.
PHP Code:
<?php
function open_file($filetype,$increment,$filename)
{
$handle = fopen($filename, 'r');
$contents = @fread($handle,filesize($filename));
fclose($handle);
If($increment == '-'){
$contents--;
}else{
$contents++;
}
$handle = fopen($filename, $filetype);
$contents = @fread($handle,filesize($filename));
fwrite($handle,$contents);
fclose($handle);
return $contents;
}
Switch($_GET['user']){
Case "+":
echo open_file('w','+','users.txt');
Case "-":
echo open_file('w','-','users.txt');
}
?>
Ive never worked with functions, so bear with me if thats the problem. Im not getting any errors but my echo isnt returning anything and the file isnt getting added to.
Im making a script to add one if in the url add=+ or minus one from the file if its add=-
Thanks :wave:
Re: Script not working, no errors though.
You read the contents. You interpret them numerically and add/substract 1. Then you read the contents again, overwriting the modified value, and write these contents back into the file.
In other words, you read the file twice, write its contents back once.
Oh, and since the second time you only open it for writing, the second read operation fails and thus you probably write garbage back.
I suppose the poor choice of name doesn't really matter anymore.
Remove the second occurrence of this line:
$contents = @fread($handle,filesize($filename));
Re: Script not working, no errors though.
Thanks cornedbee, as you typed that i figured out that i was reading it twice and killing the data.
PHP Code:
<?php
function open_file($filetype,$increment,$filename)
{
$handle = fopen($filename, 'r');
$contents = @fread($handle,filesize($filename));
fclose($handle);
If($increment == '-'){
$contents--;
}else{
$contents++;
}
$handle = fopen($filename, $filetype);
fwrite($handle,$contents);
fclose($handle);
echo $contents;
return $contents;
}
Switch($_GET['user']){
Case "+":
echo open_file('w','+','users.txt');
Case "-":
echo open_file('w','-','users.txt');
}
?>
but i dont think im even getting to the open_file function, because it refuses to echo anything, even the first $contents.
Thanks much :wave:
Re: Script not working, no errors though.
Re: Script not working, no errors though.
You don't need to echo $contents in your function, unless you want it to show up twice. Also, I doubt it's like the '+' and '-' signs in the query string, try something different:
PHP Code:
<?php
function open_file($filetype,$increment,$filename)
{
$handle = fopen($filename, 'r');
$contents = fread($handle,filesize($filename));
fclose($handle);
If($increment == 'minus'){
$contents--;
}else{
$contents++;
}
$handle = fopen($filename, $filetype);
fwrite($handle,$contents);
fclose($handle);
return $contents;
}
Switch($_GET['user']){
Case "plus":
echo open_file('w','plus','users.txt');
break;
Case "minus":
echo open_file('w','minus','users.txt');
break;
}
?>