JB
Looking For Status Send PM Posts: 21
Threads: 5 Money: £36.08 (D) (+ Friend)
|
I've created a hit counter to be viewed on a different page and it resets to zero instead of adding a number. On the increment page:
$myFile = "../../clicks/whatever.txt";
$activeFile = fopen($myFile, 'w+');
$theData = fread($activeFile, filesize($myFile));
if(!$theData){
$theData = 0;
}else{
$theData++;
}
fwrite($activeFile, $theData);
fclose($activeFile);
?>
And on the viewing page:
$myFile = "./clicks/whatever.txt";
$activeFile = fopen($myFile, 'r');
$theData = fread($activeFile, filesize($myFile));
fclose($activeFile);
echo $theData;
?>
Can anyone see any obvious mistakes cos I'm pulling my hair out!
I ran a test script and it said fread(): Length parameter must be greater than 0. It may aswell be speaking egyptian to me for the sense it makes...
Any help would be appreciated.
|
18.02.06 10:58 Post #1 | Last edited: 18.02.06 10:59 (JB - 1 times) |
[Hide Sig (0)] [Profile] [Quote] |
Zogger!
Looking For Status Send PM Posts: 3954
Threads: 62 Money: £93.82 (D) (+ Friend)
|
does the file exist? the second parameter of fread has to be > 0, and if the file doesn't exist it'll probly be 0. There are easier functions for this than fread (eg file_get_contents)...
You might be having problems with your file paths. An absolute path will make sure you're accessing the right file, instead of "./clicks/whatever.txt", try $_SERVER["DOCUMENT_ROOT"]."/clicks/whatever.txt" - presuming this is directly under your public_html directory.
________________
You know I'm a dancing machine |
18.02.06 12:20 Post #2 | Last edited: 18.02.06 12:21 (ZoGgEr! - 1 times) |
[Hide Sig (8)] [Profile] [Quote] |
JB
Looking For Status Send PM Posts: 21
Threads: 5 Money: £36.08 (D) (+ Friend)
|
The file does exist. Say I place the number 1 in the .txt file. this is displayed when I access the page. When I then view the increment page and return the parameter is then 0 and doesn't change. Some test code someone gave me to stick up there gives the following error in the display page:
Warning: fread(): Length parameter must be greater than 0. in c:\domains\quitebad.com\wwwroot\homepage.php on line 319
The error in the increment page:
Warning: fread(): Length parameter must be greater than 0. in c:\domains\quitebad.com\wwwroot\2004\racialism\index.php on line 65
The Count script cannot read the file!
I have zilch knowlege of php at present and I'm sticking up codes that people have written for me.
Take a look http://www.quitebad.com/homepage.php
Should I change the code that you suggested - it reads the text files but doesn't write the number plus 1 - as far as I can see
|
18.02.06 12:38 Post #3 | Last edited: 18.02.06 12:40 (JB - 2 times) |
[Hide Sig (0)] [Profile] [Quote] |
Zogger!
Looking For Status Send PM Posts: 3954
Threads: 62 Money: £93.82 (D) (+ Friend)
|
file permissions could be wrong then. What to set it to depends on your server, but chmod'ing the file to 777 can't fail, and you're not at any risk from one file 777d.
________________
You know I'm a dancing machine |
18.02.06 13:34 Post #4 | Last edited: 18.02.06 13:34 (ZoGgEr! - 1 times) |
[Hide Sig (8)] [Profile] [Quote] |
JB
Looking For Status Send PM Posts: 21
Threads: 5 Money: £36.08 (D) (+ Friend)
|
I assume I do that from my account control panel? Do you have any suggestions as to where to look as nothing seems very obvious to me...
I can see a database manager but that's for mysql
|
18.02.06 13:57 Post #5 | Last edited: 18.02.06 13:58 (JB - 1 times) |
[Hide Sig (0)] [Profile] [Quote] |
C1
Looking For Status Send PM Posts: 0
Threads: 0 Money: £0.18 (D) (+ Friend)
|
I assume I do that from my account control panel? Do you have any suggestions as to where to look as nothing seems very obvious to me...
I can see a database manager but that's for mysql
To chmod to 777, use a ftp program, such as Filezilla. Once you login to your website, right click on the file and click set file attributes, or something along those lines.
Check all the read, write boxes or type in the numbers 777, if there's a text box to type that.
And it shall be chmoded to 777.
Sorry, I'm terrible at telling people how to do things...but that should work.
EDIT: OMG! This is my 9000th post!
|
18.02.06 14:15 Post #6 | Last edited: 18.02.06 14:35 (C1 - 1 times) |
[Hide Sig (2)] [Profile] [Quote] |
JB
Looking For Status Send PM Posts: 21
Threads: 5 Money: £36.08 (D) (+ Friend)
|
Nope, that was explained well, I did it and I'm still getting the same problems.
Edit:
And if I alter the directory path to the suggested I get:
Warning: fopen(/clicks/jesus.txt): failed to open stream: No such file or directory in c:\domains\quitebad.com\wwwroot\homepage.php on line 151
Warning: filesize(): Stat failed for /clicks/jesus.txt (errno=2 - No such file or directory) in c:\domains\quitebad.com\wwwroot\homepage.php on line 152
Warning: fread(): supplied argument is not a valid stream resource in c:\domains\quitebad.com\wwwroot\homepage.php on line 152
Warning: fclose(): supplied argument is not a valid stream resource in c:\domains\quitebad.com\wwwroot\homepage.php on line 153
aaaaaaagggggggggghhhhhhhhhhhh...........
|
18.02.06 14:55 Post #7 | Last edited: 18.02.06 15:02 (JB - 1 times) |
[Hide Sig (0)] [Profile] [Quote] |
JB
Looking For Status Send PM Posts: 21
Threads: 5 Money: £36.08 (D) (+ Friend)
|
Does anyone know how I check that I've allowed permissions successfully? Is it the .txt files that I just allow the permissions, the directory they're in or do I do the .php files too?
Programming huh?
|
18.02.06 17:41 Post #8 | [Hide Sig (0)] [Profile] [Quote] |
routine_error
Statusless Send PM Posts: 1081
Threads: 25 Money: £6.36 (D) (+ Friend)
|
no, just unix file attribute type stuff. you can google for "chmod" and i'm sure something will come up that might help you.
if you can connect with an ftp program you should be able to chmod your txt file... i dont think ive ever used an ftp program that doesn't let you do that, but then, i haven't used very many ftp programs... good luck.
|
19.02.06 19:00 Post #9 | [Hide Sig (1)] [Profile] [Quote] |
JB
Looking For Status Send PM Posts: 21
Threads: 5 Money: £36.08 (D) (+ Friend)
|
I'm sure the files are writeable because changes happen, albeit, the wrong changes. The problem is with the code. This is what someone else said on another forum that made no sense to me at all:
comment on file modes: w+ means write first (but be able to rewind and read later)
Your scenario calls for read first, then rewind and write back modified data
$fp = fopen("counter.txt", "r+");
$data = fread($fp, 10); // expecting more counts than fit into 10 digits?
$data++;
fseek($fp, 0, 0); // rewind
fputs($fp, $data); // and write back
_____________________________________________
Yes, but how do I change the fecking code, like!
|
19.02.06 23:01 Post #10 | [Hide Sig (0)] [Profile] [Quote] |
Your Comments: