|
-
Sep 18th, 2005, 04:13 PM
#1
Thread Starter
Admodistrator
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
-
Sep 18th, 2005, 04:28 PM
#2
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));
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Sep 18th, 2005, 04:40 PM
#3
Thread Starter
Admodistrator
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
-
Sep 19th, 2005, 04:35 PM
#4
Thread Starter
Admodistrator
Re: Script not working, no errors though.
-
Sep 19th, 2005, 06:20 PM
#5
Lively Member
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;
}
?>
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|