Jump to content

File Problem


Sniffy
 Share

Recommended Posts

I'm trying to make a hit counter for my homepage with a text file(counter.txt).What went wrong?The error

Parse error: syntax error, unexpected T_VARIABLE in /home/www/sniffy.awardspace.com/index.php on line 51

The page(index.php)

<?phpif(isset($_COOKIE['admin']))$adminCheck = "<font face='Comic Sans MS'>Welcome back,".$_COOKIE['admin'].".<br><a href='updateIndex.php'>Update the FrontPage.</a></font>";else$adminCheck = "<a href='adminLogin.php'>Admin Login</a>";?><html><head><meta http-equiv="Content-Language" content="en-us"><meta name="GENERATOR" content="Microsoft FrontPage 5.0"><meta name="ProgId" content="FrontPage.Editor.Document"><meta http-equiv="Content-Type" content="text/html; charset=windows-1252"><title>Bobville</title><link rel='stylesheet' type='text/css' href='style.css'></head><body><img src='logo.gif' width='100%' height='40'><br><table cellspacing='0' width='100%'><tr><td width='200'><div class='menu'><a href='index.php'>Home</a><br><a href='games/index.php'>Games</a><br><a href='movies/index.php'>Movies</a><br><a href='tutorials/index.php'>Tutorials</a><br><a href='links.php'>Links</a></td><font size="2"><td width='600'></font><font face="Comic Sans MS"><i><font size="2">Updates</font>:</i></font><br><br><?php$fileName = "updates.txt";$fileHandle = fopen($fileName, 'w+');$fileContents = fread($fileHandle, filesize($fileName));fclose($fileHandle);echo $fileContents;?></td><td width='200'><div class='menu'><?phpecho $adminCheck;?><br><?php$counterF = "counter.txt"$counterH = fopen($counterF, 'r+');$counterR = fread($counterH, filesize($counterF));fwrite($counterH, $counterR+­+);echo $counterR;?></td></tr></table></body></html>

The counter.txt file

0

Link to comment
Share on other sites

Actually, there's another problem.Error:

Parse error: syntax error, unexpected ')' in /home/www/sniffy.awardspace.com/index.php on line 53

<?phpif(isset($_COOKIE['admin']))$adminCheck = "<font face='Comic Sans MS'>Welcome back,".$_COOKIE['admin'].".<br><a href='updateIndex.php'>Update the FrontPage.</a></font>";else$adminCheck = "<a href='adminLogin.php'>Admin Login</a>";?><html><head><meta http-equiv="Content-Language" content="en-us"><meta name="GENERATOR" content="Microsoft FrontPage 5.0"><meta name="ProgId" content="FrontPage.Editor.Document"><meta http-equiv="Content-Type" content="text/html; charset=windows-1252"><title>Bobville</title><link rel='stylesheet' type='text/css' href='style.css'></head><body><img src='logo.gif' width='100%' height='40'><br><table cellspacing='0' width='100%'><tr><td width='200'><div class='menu'><a href='index.php'>Home</a><br><a href='games/index.php'>Games</a><br><a href='movies/index.php'>Movies</a><br><a href='tutorials/index.php'>Tutorials</a><br><a href='links.php'>Links</a></td><font size="2"><td width='600'></font><font face="Comic Sans MS"><i><font size="2">Updates</font>:</i></font><br><br><?php$fileName = "updates.txt";$fileHandle = fopen($fileName, 'w+');$fileContents = fread($fileHandle, filesize($fileName));fclose($fileHandle);echo $fileContents;?></td><td width='200'><div class='menu'><?phpecho $adminCheck;?><br><?php$counterF = "counter.txt";$counterH = fopen($counterF, 'r+');$counterR = fread($counterH, filesize($counterF));fwrite($counterH, $counterR+­+);fclose($counterH);echo $counterR;?></td></tr></table></body></html>

Oh, I fixed the ++ increment problem, but now my main content has an fread() parameter error on line 38 and my counter adds one zero to the file like 0,00,000 instead of 1, 2, 3, 4.Can you help me fix these problems?

<?phpif(isset($_COOKIE['admin']))$adminCheck = "<font face='Comic Sans MS'>Welcome back,".$_COOKIE['admin'].".<br><a href='updateIndex.php'>Update the FrontPage.</a></font>";else$adminCheck = "<a href='adminLogin.php'>Admin Login</a>";?><html><head><meta http-equiv="Content-Language" content="en-us"><meta name="GENERATOR" content="Microsoft FrontPage 5.0"><meta name="ProgId" content="FrontPage.Editor.Document"><meta http-equiv="Content-Type" content="text/html; charset=windows-1252"><title>Bobville</title><link rel='stylesheet' type='text/css' href='style.css'></head><body><img src='logo.gif' width='100%' height='40'><br><table cellspacing='0' width='100%'><tr><td width='200'><div class='menu'><a href='index.php'>Home</a><br><a href='games/index.php'>Games</a><br><a href='movies/index.php'>Movies</a><br><a href='tutorials/index.php'>Tutorials</a><br><a href='links.php'>Links</a></td><font size="2"><td width='600'></font><font face="Comic Sans MS"><i><font size="2">Updates</font>:</i></font><br><br><?php$fileName = "updates.txt";$fileHandle = fopen($fileName, 'r');$fileContents = fread($fileHandle, filesize($fileName));fclose($fileHandle);echo $fileContents;?></td><td width='200'><div class='menu'><?phpecho $adminCheck;?><br><?php$counterF = "counter.txt";$counterH = fopen($counterF, 'w+');$counterR = fread($counterH, filesize($counterF));fwrite($counterH, $counterR+­1);fclose($counterH);echo $counterR;?></td></tr></table></body></html>

Note: I'm getting two fread() parameter 0 errors.

Link to comment
Share on other sites

Add this to the top:error_reporting(E_ALL);and paste all of the errors that appear. For the string concatenation, change your fread line to this (assuming fread is working correctly) to force the value to be a number instead of a string.$counterR = intval(fread($counterH, filesize($counterF)));

Link to comment
Share on other sites

Warning: filesize(): Stat failed for Resource id #1 (errno=2 - No such file or directory) in /home/www/sniffy.awardspace.com/index.php on line 41Warning: fread(): Length parameter must be greater than 0. in /home/www/sniffy.awardspace.com/index.php on line 41

Warning: filesize(): Stat failed for Resource id #2 (errno=2 - No such file or directory) in /home/www/sniffy.awardspace.com/index.php on line 55Warning: fread(): Length parameter must be greater than 0. in /home/www/sniffy.awardspace.com/index.php on line 55Notice: Use of undefined constant ­1 - assumed '­1' in /home/www/sniffy.awardspace.com/index.php on line 560

Link to comment
Share on other sites

That's not the problem though, I made sure I checked that the .txt files were there.Here's my exact code by the way.

<?phperror_reporting(E_ALL);?><?phpif(isset($_COOKIE['admin']))$adminCheck = "<font face='Comic Sans MS'>Welcome back,".$_COOKIE['admin'].".<br><a href='updateIndex.php'>Update the FrontPage.</a></font>";else$adminCheck = "<a href='adminLogin.php'>Admin Login</a>";?><html><head><meta http-equiv="Content-Language" content="en-us"><meta name="GENERATOR" content="Microsoft FrontPage 5.0"><meta name="ProgId" content="FrontPage.Editor.Document"><meta http-equiv="Content-Type" content="text/html; charset=windows-1252"><title>Bobville</title><link rel='stylesheet' type='text/css' href='style.css'></head><body><img src='logo.gif' width='100%' height='40'><br><table cellspacing='0' width='100%'><tr><td width='200'><div class='menu'><a href='index.php'>Home</a><br><a href='games/index.php'>Games</a><br><a href='movies/index.php'>Movies</a><br><a href='tutorials/index.php'>Tutorials</a><br><a href='links.php'>Links</a></td><font size="2"><td width='600'></font><font face="Comic Sans MS"><i><font size="2">Updates</font>:</i></font><br><br><?php$fileName = "updates.txt";$fileHandle = fopen($fileName, 'r')or die("Could not open file");$fileContents = fread($fileHandle, filesize($fileHandle));fclose($fileHandle);echo $fileContents;?></td><td width='200'><div class='menu'><?phpecho $adminCheck;?><br><?php$counterF = "counter.txt";$counterH = fopen($counterF, 'w+')or die("Could not open file");$counterR = intval(fread($counterH, filesize($counterH)));fwrite($counterH, $counterR+­1);fclose($counterH);echo $counterR;?></td></tr></table></body></html>

Link to comment
Share on other sites

You have to believe the error messages. The function is not able to find the file, that's what it's telling you. You might see the file there, but the function cannot find it.Print out the filename right before you check to make sure you know what you are telling it to look for. If you aren't including a path, the file needs to be in the same directory as the running script. If it is, and still can't find it, then include a path with the filename instead of just the filename.

Link to comment
Share on other sites

Like this?

<?phperror_reporting(E_ALL);?><?phpif(isset($_COOKIE['admin']))$adminCheck = "<font face='Comic Sans MS'>Welcome back,".$_COOKIE['admin'].".<br><a href='updateIndex.php'>Update the FrontPage.</a></font>";else$adminCheck = "<a href='adminLogin.php'>Admin Login</a>";?><html><head><meta http-equiv="Content-Language" content="en-us"><meta name="GENERATOR" content="Microsoft FrontPage 5.0"><meta name="ProgId" content="FrontPage.Editor.Document"><meta http-equiv="Content-Type" content="text/html; charset=windows-1252"><title>Bobville</title><link rel='stylesheet' type='text/css' href='style.css'></head><body><img src='logo.gif' width='100%' height='40'><br><table cellspacing='0' width='100%'><tr><td width='200'><div class='menu'><a href='index.php'>Home</a><br><a href='games/index.php'>Games</a><br><a href='movies/index.php'>Movies</a><br><a href='tutorials/index.php'>Tutorials</a><br><a href='links.php'>Links</a></td><font size="2"><td width='600'></font><font face="Comic Sans MS"><i><font size="2">Updates</font>:</i></font><br><br><?php$fileName = "http://sniffy.awardspace.com/updates.txt";$fileHandle = fopen($fileName, 'r')or die("Could not open file");$fileContents = fread($fileHandle, filesize($fileHandle));fclose($fileHandle);echo $fileContents;?></td><td width='200'><div class='menu'><?phpecho $adminCheck;?><br><?php$counterF = "http://sniffy.awardspace.com/counter.txt";$counterH = fopen($counterF, 'w+')or die("Could not open file");$counterR = intval(fread($counterH, filesize($counterH)));fwrite($counterH, $counterR+­1);fclose($counterH);echo $counterR;?></td></tr></table></body></html>

errors

Warning: fopen(http://sniffy.awardspace.com/counter.txt): failed to open stream: HTTP wrapper does not support writeable connections. in /home/www/sniffy.awardspace.com/index.php on line 54Could not open file

Warning: filesize(): Stat failed for Resource id #1 (errno=2 - No such file or directory) in /home/www/sniffy.awardspace.com/index.php on line 41Warning: fread(): Length parameter must be greater than 0. in /home/www/sniffy.awardspace.com/index.php on line 41

Link to comment
Share on other sites

You can use the HTTP path to read a file, but not to write it. So, for the counter file you are trying to write you need to use the filesystem path. You should probably also use it for the update file you are trying to read, it will be faster. The error message says this is the file with the problem:/home/www/sniffy.awardspace.com/index.phpSo your counter file would be this:/home/www/sniffy.awardspace.com/counter.txtBut, some web hosts use virtual paths that don't work with the file functions. If you try using that path, and it still says it can't find it, print the value of this variable to find out what the actual path is instead of the virtual path:echo __FILE__;

Link to comment
Share on other sites

Still having problems.

<?phperror_reporting(E_ALL);?><?phpif(isset($_COOKIE['admin']))$adminCheck = "<font face='Comic Sans MS'>Welcome back,".$_COOKIE['admin'].".<br><a href='updateIndex.php'>Update the FrontPage.</a></font>";else$adminCheck = "<a href='adminLogin.php'>Admin Login</a>";?><html><head><meta http-equiv="Content-Language" content="en-us"><meta name="GENERATOR" content="Microsoft FrontPage 5.0"><meta name="ProgId" content="FrontPage.Editor.Document"><meta http-equiv="Content-Type" content="text/html; charset=windows-1252"><title>Bobville</title><link rel='stylesheet' type='text/css' href='style.css'></head><body><img src='logo.gif' width='100%' height='40'><br><table cellspacing='0' width='100%'><tr><td width='200'><div class='menu'><a href='index.php'>Home</a><br><a href='games/index.php'>Games</a><br><a href='movies/index.php'>Movies</a><br><a href='tutorials/index.php'>Tutorials</a><br><a href='links.php'>Links</a></td><font size="2"><td width='600'></font><font face="Comic Sans MS"><i><font size="2">Updates</font>:</i></font><br><br><?php$fileName = "http://sniffy.awardspace.com/updates.txt";$fileHandle = fopen($fileName, 'r')or die("Could not open file");$fileContents = fread($fileHandle, filesize($fileHandle));fclose($fileHandle);echo $fileContents;?></td><td width='200'><div class='menu'><?phpecho $adminCheck;?><br><?php$counterF = "/home/www/sniffy.awardspace.com/counter.txt";$counterH = fopen($counterF, 'w+')or die("Could not open file");$counterR = intval(fread($counterH, filesize($counterH)));fwrite($counterH, $counterR+­1);fclose($counterH);echo $counterR;?><?phpecho __FILE__;?></td></tr></table></body></html>

errors

Warning: filesize(): Stat failed for Resource id #2 (errno=2 - No such file or directory) in /home/www/sniffy.awardspace.com/index.php on line 55Warning: fread(): Length parameter must be greater than 0. in /home/www/sniffy.awardspace.com/index.php on line 55Notice: Use of undefined constant ­1 - assumed '­1' in /home/www/sniffy.awardspace.com/index.php on line 560/home/www/sniffy.awardspace.com/index.php

/home/www/MY DOMAIN is the path I think.I tried adding a '0' in front of it but it didn't work.

Link to comment
Share on other sites

OK, just for clarification we'll print out a few things. Absolute path names in Unix always start with a / so the 0 would not be part of the path.

echo "\$_SERVER['PATH_TRANSLATED']: \"{$_SERVER['PATH_TRANSLATED']}\"<br>";echo "\$_SERVER['APPL_PHYSICAL_PATH']: \"{$_SERVER['APPL_PHYSICAL_PATH']}\"<br>";echo "\$_SERVER['REQUEST_URI']: \"{$_SERVER['REQUEST_URI']}\"<br>";echo "\$_SERVER['SCRIPT_FILENAME']: \"{$_SERVER['SCRIPT_FILENAME']}\"<br>";echo "\$_SERVER['ORIG_PATH_TRANSLATED']: \"{$_SERVER['ORIG_PATH_TRANSLATED']}\"<br>";

Link to comment
Share on other sites

Warning: filesize(): Stat failed for Resource id #2 (errno=2 - No such file or directory) in /home/www/sniffy.awardspace.com/index.php on line 55Warning: fread(): Length parameter must be greater than 0. in /home/www/sniffy.awardspace.com/index.php on line 55Notice: Use of undefined constant ­1 - assumed '­1' in /home/www/sniffy.awardspace.com/index.php on line 560Notice: Undefined index: PATH_TRANSLATED in /home/www/sniffy.awardspace.com/index.php on line 61$_SERVER['PATH_TRANSLATED']: ""Notice: Undefined index: APPL_PHYSICAL_PATH in /home/www/sniffy.awardspace.com/index.php on line 62$_SERVER['APPL_PHYSICAL_PATH']: ""$_SERVER['REQUEST_URI']: "/"$_SERVER['SCRIPT_FILENAME']: "/home/www/sniffy.awardspace.com/index.php"Notice: Undefined index: ORIG_PATH_TRANSLATED in /home/www/sniffy.awardspace.com/index.php on line 65$_SERVER['ORIG_PATH_TRANSLATED']: ""

Would the path be ../home/www/sniffyawardspace.com/counter.txt ?

Link to comment
Share on other sites

The 'updates' section works not but not the counter.

<?phperror_reporting(E_ALL);?><?phpif(isset($_COOKIE['admin']))$adminCheck = "<font face='Comic Sans MS'>Welcome back,".$_COOKIE['admin'].".<br><a href='updateIndex.php'>Update the FrontPage.</a></font>";else$adminCheck = "<a href='adminLogin.php'>Admin Login</a>";?><html><head><meta http-equiv="Content-Language" content="en-us"><meta name="GENERATOR" content="Microsoft FrontPage 5.0"><meta name="ProgId" content="FrontPage.Editor.Document"><meta http-equiv="Content-Type" content="text/html; charset=windows-1252"><title>Bobville</title><link rel='stylesheet' type='text/css' href='style.css'></head><body><img src='logo.gif' width='100%' height='40'><br><table cellspacing='0' width='100%'><tr><td width='200'><div class='menu'><a href='index.php'>Home</a><br><a href='games/index.php'>Games</a><br><a href='movies/index.php'>Movies</a><br><a href='tutorials/index.php'>Tutorials</a><br><a href='links.php'>Links</a></td><font size="2"><td width='600'></font><font face="Comic Sans MS"><i><font size="2">Updates</font>:</i></font><br><br><?php$fileName = "/home/www/sniffy.awardspace.com/updates.txt";$fileHandle = fopen($fileName, 'r')or die("Could not open file");$fileContents = fread($fileHandle, filesize($fileName));fclose($fileHandle);echo $fileContents;?></td><td width='200'><div class='menu'><?phpecho $adminCheck;?><br><?php$counterF = "/home/www/sniffy.awardspace.com/counter.txt";$counterH = fopen($counterF, 'w+')or die("Could not open file");$counterR = intval(fread($counterH, filesize($counterF)));fwrite($counterH, $counterR+­1);fclose($counterH);echo $counterR;?></td></tr></table></body></html>

Warning: fread(): Length parameter must be greater than 0. in /home/www/sniffy.awardspace.com/index.php on line 55Notice: Use of undefined constant 1 - assumed '1' in /home/www/sniffy.awardspace.com/index.php on line 560

Link to comment
Share on other sites

Then apparently the filesize is 0, because it is complaining that you are trying to read 0 bytes.All I'm doing here is reading the error messages, they will tell you exactly what the problem is.

fread(): Length parameter must be greater than 0
That means that the length parameter to fread is 0. The length parameter is the call to filesize. So, apparently, filesize is returning a 0. So the file probably doesn't have anything in it. Open up the file, and put a 0 or a 1 in there and save it again.
Link to comment
Share on other sites

Well, the error says that the length parameter for fread is a 0. So, look at this line:$counterR = intval(fread($counterH, filesize($counterF)));So, it saying that filesize is returning 0. So print the filename and the results of filesize and see what it is doing:

$length = filesize($counterF);echo "filename: \"{$counterF}\"<br>";echo "length: ";var_dump($length);

Link to comment
Share on other sites

I don't think so. The length is coming back as an integer, which it should be. A length wouldn't make sense if it weren't a number. I just wanted to check if it would be int(0) or bool(false). So, at least it is the int. So, this is saying that the file is empty, that the filesize is 0 bytes. I guess I don't know what else to say, it is reading the counter.txt file and saying that it is 0 bytes, so I have to assume that is true. Upload a new counter.txt with 100 in it or something and see what happens.

Link to comment
Share on other sites

It still doesn't work, but I got rid of all the errors with this code.It doesn't update it though. I guess filesize() just didn't work for some reason.

<?phperror_reporting(E_ALL);?><?phpif(isset($_COOKIE['admin']))$adminCheck = "<font face='Comic Sans MS'>Welcome back,".$_COOKIE['admin'].".<br><a href='updateIndex.php'>Update the FrontPage.</a></font>";else$adminCheck = "<a href='adminLogin.php'>Admin Login</a>";?><html><head><meta http-equiv="Content-Language" content="en-us"><meta name="GENERATOR" content="Microsoft FrontPage 5.0"><meta name="ProgId" content="FrontPage.Editor.Document"><meta http-equiv="Content-Type" content="text/html; charset=windows-1252"><title>Bobville</title><link rel='stylesheet' type='text/css' href='style.css'></head><body><img src='logo.gif' width='100%' height='40'><br><table cellspacing='0' width='100%'><tr><td width='200'><div class='menu'><a href='index.php'>Home</a><br><a href='games/index.php'>Games</a><br><a href='movies/index.php'>Movies</a><br><a href='tutorials/index.php'>Tutorials</a><br><a href='links.php'>Links</a></td><font size="2"><td width='600'></font><font face="Comic Sans MS"><i><font size="2">Updates</font>:</i></font><br><br><?php$fileName = "/home/www/sniffy.awardspace.com/updates.txt";$fileHandle = fopen($fileName, 'r')or die("Could not open file");$fileContents = fread($fileHandle, filesize($fileName));fclose($fileHandle);echo $fileContents;?></td><td width='200'><div class='menu'><?phpecho $adminCheck;?><br><?php$counterF = "/home/www/sniffy.awardspace.com/counter_homepage.txt";$counterH = fopen($counterF, 'w+')or die("Could not open file");$counterR = intval(fread($counterH, 5));fwrite($counterH, $counterR+1);fclose($counterH);echo $counterR;?><!--<?php$length = filesize($counterF);echo "filename: \"{$counterF}\"<br>";echo "length: ";var_dump($length);?>!--></td></tr></table></body></html>

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...