-1

I can't tell you how stupid I feel having to ask this question, but I've been working on the most simple of commands (for two days) and can't find the problem. I 'borrowed' some code for a non repeating hit counter. I have tried to get it to work and I finally determined I'm not getting access to the simple txt files that store the hits or the one that stores the ip addresses. I've read the problems here, looked at the command in a 'Dummies' book and even watched YouTube videos and I'm blind to the problem. I've tried using a string for the file name and using the filename directly. I had the files in a sub folder on the server and thought that might be the issue so I moved them to the root with the same error. If someone can see why this isn't working I'd be eternally grateful.

This is only part of the whole code but it's where I determined that it fails.

        $filename = 'countfix.txt';     
            $handle = fopen('$filename', 'r');
            fread($handle, $current_inc)
                or die ("Can't open file");
                echo $current_inc;
            fclose($handle);

Thanks.

Damodaran
  • 10,882
  • 10
  • 60
  • 81
Jim Raymond
  • 103
  • 9
  • 1
    PHP resolves paths relative to the root of the server, not the root of the web site. I commonly construct the full path to the file using '$_SERVER["DOCUMENT_ROOT"]' as the leading part of the path to the web site. So if your file is at the root of your website (/countfix.txt) you would use $_SERVER["DOCUMENT_ROOT"]/countfix.txt – Andrew - OpenGeoCode Nov 30 '13 at 15:53
  • Sorry @Andrew I misspoke. The site I'm working on has a separate folder. I made a sub folder under that and thought it would work because all my files for this counter were in there. Then I read something about putting the path in, so I moved the files out to the main folder, but as we can see, that was not the issue. – Jim Raymond Nov 30 '13 at 16:26

2 Answers2

5

This is wrong:

$handle = fopen('$filename', 'r'); // tries to open a file named $filename

It should be written this way:

$handle = fopen($filename, 'r');   // no quotes, opens countfix.txt

You might have meant to write this instead:

$handle = fopen("$filename", 'r');

wherein the double quotes will cause the real value of $filename to be substituted into the string (thus making the code work), but there is no point in doing that. Lose the quotes.

Additionally, this code doesn't do what it says:

fread($handle, $current_inc) or die ("Can't open file");

Here the error message is printed if you cannot read from the file, not when you fail to open it. You should check the return value of fopen instead or modify the message to be more accurate.

Jon
  • 428,835
  • 81
  • 738
  • 806
  • Jon your answer made perfect sense. However when I took the single quotes out it still won't work. I played around with a number of things and still can't get it to read the file. I changed the error reporting and now apparently it is opening the file, but it can't read it, for some reason. I hate to be a pest for something that should be so simple! $filename = 'countfix.txt'; $handle = fopen($filename, 'r') or die ("Can't open file"); fread($handle, ($current_inc)) or die ("Can't read file"); echo $answer; fclose($handle); – Jim Raymond Nov 30 '13 at 16:58
  • @JimRaymond: Turn the `display_errors` and `error_reporting` settings up so that you get feedback in the form of warning messages. It's not possible to guess all the way through the solution. – Jon Nov 30 '13 at 17:58
  • The business server I was working on did not allow me to access the php.ini files. So I moved the files to the web server I rent. Most of the error reporting was turned on and the ones I could find turned off I did turn on. But it still didn't report any errors. Now I'm starting from scratch and playing with the 'f' commands (fopen, fread, etc.) and hoping that when I fully understand it I can find the problem. Thanks. – Jim Raymond Dec 01 '13 at 12:06
1

This is the right way to do it:

$handle = fopen("$filename", 'r');

You must enclose variables with doubble quotes, or not enclose them at all! This is how PHP is.

You might want to read this: what is the difference between single quoted and double quoted strings in php

Community
  • 1
  • 1
131
  • 1,363
  • 1
  • 12
  • 32
  • bad advice, @jon got it better. Lose the quotes, stop putting variables in quotes... especially in this case – hanzo2001 Nov 30 '13 at 15:51
  • 1
    This answer is correct. It does not at least deserve a dovn-vote even if it is not the best one – Hanky Panky Nov 30 '13 at 15:51
  • Thanks Hanky. I think you're right, if Jon's answer is the best one, the one who asked the question, will accept it as the best answer. – 131 Nov 30 '13 at 15:54
  • 1
    Ahmad, thanks for the help. I tried that and it didn't work either. But I am eternally grateful for the link. For us newbies it's a constant battle what to use ' vs " and ( vs { vs [. At least now I have a place (bookmarked) to go read when I hit a problem. I had searched the net but never found that one. Thanks. – Jim Raymond Nov 30 '13 at 17:00