Asday Posted July 5, 2007 Share Posted July 5, 2007 I have this code: <?php $fp=fopen("count.txt", "w"); fread($fp, $count194); $count194++; fwrite($fp, $count194); echo $count194; ?> Which is meant to count + 1 every time the page is accessed. As I said, simple. At the moment, I don't want to know if there's a better way to do it. I just want to know why this goes wrong. The count starts at 0, and goes up to 1, but then doesn't go any higher, even with cache-less refreshes. Thanks for looking Quote Link to comment Share on other sites More sharing options...
teng84 Posted July 5, 2007 Share Posted July 5, 2007 no loop or the variable values reset because the page maybe refresh Quote Link to comment Share on other sites More sharing options...
NArc0t1c Posted July 5, 2007 Share Posted July 5, 2007 <?php $fp=fopen("count.txt", "w"); fread($fp, $count); $count = $count+1; fwrite($fp, $count); echo $count; ?> Quote Link to comment Share on other sites More sharing options...
Yesideez Posted July 5, 2007 Share Posted July 5, 2007 $count = $count+1; That is exactly the same as $count++; Quote Link to comment Share on other sites More sharing options...
Asday Posted July 5, 2007 Author Share Posted July 5, 2007 no loop or the variable values reset because the page maybe refresh But I'm saving to a file, which stores the value for later use... EDIT: Just checked. I changed the value in the file to 1651, and the echo still outputs 1. Quote Link to comment Share on other sites More sharing options...
Yesideez Posted July 5, 2007 Share Posted July 5, 2007 Try this: <?php $fp=fopen("count.txt", "w"); $count194=fread($fp, filesize("count.txt")); $count194++; fwrite($fp, $count194); echo $count194; ?> Quote Link to comment Share on other sites More sharing options...
Asday Posted July 5, 2007 Author Share Posted July 5, 2007 Try this: <?php $fp=fopen("count.txt", "w"); $count194=fread($fp, filesize("count.txt")); $count194++; fwrite($fp, $count194); echo $count194; ?> What does the bit you changed mean/meant to do. Also, that doesn't work. Thanks anyway! EDIT: Ooh, editing posts? cheating! EDIT2: Nope, that deletes the contents of the file. Quote Link to comment Share on other sites More sharing options...
teng84 Posted July 5, 2007 Share Posted July 5, 2007 <? $filename = "some.dat" ; $dataFile = fopen( $filename, "r" ) ; if ( $dataFile ) { while (!feof($dataFile)) { $buffer = fgets($dataFile, 4096); echo $buffer; echo $ctr+=1; } fclose($dataFile); } else { die( "fopen failed for $filename" ) ; } try that sample Quote Link to comment Share on other sites More sharing options...
Yesideez Posted July 5, 2007 Share Posted July 5, 2007 With fread the second parameter should be the number of bytes you want to read from the file and the value returned is the string read from the file or false on failure. Let's have a go at some error checking: <?php if ($fp=fopen("count.txt", "r+")) { if ($count194=fread($fp, filesize("count.txt"))) { echo 'Counter before: '.$count194.'<br />'; $count194++; fwrite($fp, $count194); echo 'Counter after: '.$count194; } else {echo 'Unable to read from file';} } else {echo 'Unable to open the file';} ?> This will tell you what it is reading from the file. I've also changed the open mode to "r+" which opens the file for reading and writing as you only had it open for writing only. Quote Link to comment Share on other sites More sharing options...
Asday Posted July 5, 2007 Author Share Posted July 5, 2007 <? $filename = "some.dat" ; $dataFile = fopen( $filename, "r" ) ; if ( $dataFile ) { while (!feof($dataFile)) { $buffer = fgets($dataFile, 4096); echo $buffer; echo $ctr+=1; } fclose($dataFile); } else { die( "fopen failed for $filename" ) ; } try that sample Well, that tried to count to infinity. Thanks for that. With fread the second parameter should be the number of bytes you want to read from the file and the value returned is the string read from the file or false on failure. Let's have a go at some error checking: <?php if ($fp=fopen("count.txt", "r+")) { if ($count194=fread($fp, filesize("count.txt"))) { echo 'Counter before: '.$count194.'<br />'; $count194++; fwrite($fp, $count194); echo 'Counter after: '.$count194; } else {echo 'Unable to read from file';} } else {echo 'Unable to open the file';} ?> This will tell you what it is reading from the file. I've also changed the open mode to "r+" which opens the file for reading and writing as you only had it open for writing only. Just about to try that. EDIT: "Unable to read from file". I commented out the reamaining "echo $count194;" simply because it was there, and I didn't know why. Quote Link to comment Share on other sites More sharing options...
Yesideez Posted July 5, 2007 Share Posted July 5, 2007 btw, having a file open for writing only and trying to read from it will result in nothing. Adding 1 to nothing will always be 1 which is why you were always getting 1. It's also a good idea to get into the practice of closing the file when you've finished with it using fclose($fp); By adding error checking you can also see where scripts go wrong easier plus it's the best way to handle errors. Another good habit to get into. Quote Link to comment Share on other sites More sharing options...
Asday Posted July 5, 2007 Author Share Posted July 5, 2007 I'm running windoze, by the way. Quote Link to comment Share on other sites More sharing options...
Yesideez Posted July 5, 2007 Share Posted July 5, 2007 Did my script work ok? Quote Link to comment Share on other sites More sharing options...
Asday Posted July 5, 2007 Author Share Posted July 5, 2007 Did my script work ok? ""Unable to read from file". I commented out the reamaining "echo $count194;" simply because it was there, and I didn't know why." Quote Link to comment Share on other sites More sharing options...
Yesideez Posted July 5, 2007 Share Posted July 5, 2007 OK now we know it isn't even reading the file. Is it there on the web space? Can you see it via FTP? Quote Link to comment Share on other sites More sharing options...
Yesideez Posted July 5, 2007 Share Posted July 5, 2007 oops ignore that - you're using Windoze - just read back! As you're running from Windoze (lol) change the open mode from "r+" to "r+b" and try again. I just read up on fread in the PHP manual and it says use "b" if running on a system that differentiates between text and binary files. Quote Link to comment Share on other sites More sharing options...
NArc0t1c Posted July 6, 2007 Share Posted July 6, 2007 Try a+, If file creates, read and write from/to it. if it doesn't exist, create it. $fp=fopen("count.txt", "a+"); Quote Link to comment Share on other sites More sharing options...
Asday Posted July 6, 2007 Author Share Posted July 6, 2007 Haha, forgot to set $count194! <?php $fp=fopen("count.txt", "r+b"); $count194=fread($fp, filesize("count.txt")); $count194++; fwrite($fp, $count194); echo $count194; ?> But... That does something silly. "1" "2" "13" "1214" "12131215" "1213121412130000" "1.21312141213E+31" "1.21312141213E+63" "0121312141213121512131214121300001.21312141213E+311.21312141213E+64" "0121312141213121512131214121300001.21312141213E+311.21312141213E+630121312141213121512131214121300001.21312141213E+311.21312141213E+65" Wtf? Quote Link to comment Share on other sites More sharing options...
Asday Posted July 6, 2007 Author Share Posted July 6, 2007 O_o w00t, did it! Spoke to a PHP genius at work (I'm on work ex) and he came up with the idea, open, read, close, unlink, increment, echo, touch, open, write. r+b worked, in the end, thanks Yesideez. On the site I use it on, I'll squeeze in credits, if I can. It's only a counter, remember. (^-^ ) Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.