Simple fwrite() to text file not working right

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
louie55
Forum Newbie
Posts: 15
Joined: Tue Jan 11, 2005 10:58 pm

Simple fwrite() to text file not working right

Post by louie55 »

I have a page that has multiple thumbnail images on it. What I am trying to do is create a PHP script that will keep track of each image viewed, then count how many times each image was viewed and display it on a statistics page. I have it sort of working, however, weird things are happening. Maybe you more experienced coders can shed some light on the subject.

Just so you completely understand what I'm trying to pull off here, I'll start from the beginning:

PHP Version: 4.4.7

Each image has an onClick event looking like this:

Code: Select all

onClick="MM_openBrWindow('salebill_files/machinerypics/jdb.jpg','','')"
When someone clicks on a thumbnail image, it calls this JavaScript Function:

Code: Select all

function MM_openBrWindow(theURL,winName,features) {
   window.open("viewpic.php?url="+theURL,"machinerypic","width=827,height=641,scrollbars=1");
}
This opens viewpic.php which grabs the Picture URL from the GET variable and splits it into parts so that just the JPG filename is left. This filename is then written to the end of a text file using fwrite(), then the browser is redirected to the JPG image using a Header statement. As you can see from the code, there are a couple IP addresses that I want excluded from this action ( I have changed the addresses ).

Code: Select all

<?php
 
$ip=$_SERVER['REMOTE_ADDR'];
 
 
if(isset($_GET['url'])){
 
   if($ip!="192.168.0.235"&&$ip!="10.0.0.64"){
      $url=$_GET['url'];
      $pic=split("/",$url);
      $picname=$pic[2];
 
      $file=fopen("picturecount.txt","a+");
      $picname=trim($picname);
      fwrite($file, $picname."\r\n");
      fclose($file);
   }
 
}
else{echo "This file is not meant to be open directly!";}
 
 
header( 'Location: '.$url ) ;
 
?>
That is all the code I am using to write these picture filenames to the text file. My intention is to have a text file with a list of all the pictures viewed, one on each line. I have another PHP file that counts the lines and displays picture view statistics, but I won't post that here for now as it does not appear to be part of my problem and is working correctly.

Now to the problem:

The above code seems to work perfectly fine when I test it. However, once I put it live on the site with hundreds of visitors and they start viewing images, weird things happen. First of which is my text file once in a while has a bunch of NULL characters in front of the picture filename. This does not happen on every line, only on some lines and it is not the same amount of NULL characters on each line. It can be anywhere from 10 NULLS to 50 or more. I have no idea how this happens as you can see I even added a trim() statement to try to get rid of the problem, but that did not work. Now, if this was the only problem, I wouldn't be worried, because the file that reads these lines trims those NULLs off and counts the pictures no problem. The REALLY weird part is, sometimes data is DELETED from the file. For instance, my picture count will be 120, then I refresh the counter and it will DROP to 104 out of no where. Then it will work for a few hours counting up, then the number will drop again out of nowhere. And it is not a malfunctioning counting script causing the problem. The actual text file is LOSING lines.

Here is a mockup of what the text file looks like when I view it in Notepad++. I could not copy and paste it directly because the NULL characters won't paste. This is not the whole file as I didn't want to put all 110 lines. Obviously, I typed in the NULL characters manually, otherwise you wouldn't be able to see them. Also, the real text file has a lot more NULLs on some lines, but I didn't put so many here so the lines wouldn't look messed up. You can get the general idea with this excerpt:

Code: Select all

jd6500sprayer-1.jpg
fhf106stackmover.jpg
[NULL][NULL][NULL][NULL][NULL][NULL][NULL][NULL][NULL][NULL][NULL]jd4010.jpg
ihc460.jpg
caseih7130.jpg
jdspreader.jpg
caseih7230.jpg
ihc3288.jpg
jd4440wloader.jpg
jd246streetbrush.jpg
3ptforklift.jpg
jd7100planter.jpg
bluejet5shankripper.jpg
[NULL][NULL][NULL][NULL][NULL][NULL][NULL][NULL]gnuse3ptbucket.jpg
[NULL][NULL][NULL][NULL][NULL][NULL][NULL][NULL][NULL][NULL][NULL]jd7410.jpg
[NULL][NULL][NULL][NULL][NULL][NULL][NULL][NULL][NULL][NULL]jd7100planter.jpg
jd7200planter.jpg
jd4440wloader.jpg
jd7200planter.jpg
jd3010lp.jpg
jd4020.jpg
miskinm7000scraper.jpg
miskinm4800scraper.jpg

I have ruled out the counting script as being my problem because it does not make any changes to the text file whatsoever, it only reads it with a file_get_contents(). It uses no fopen() or fwrite(). I don't know how this can be happening?? I can NOT reproduce the problem myself. No matter how many pictures I click, I can not get it to write NULLs or to erase anything. It only happens when the public starts starts viewing pictures when the page is live. Could it maybe have something to do with search engine bots? I'm at a loss for an explanation.

Any ideas? Maybe I should go about it a different way? I suppose I could resort to a MySQL database if I have to, but I see no reason why I shouldn't be able to use a text file to perform this simple task.
rimian
Forum Newbie
Posts: 9
Joined: Wed Apr 30, 2008 5:33 am

Re: Simple fwrite() to text file not working right

Post by rimian »

Sounds like you have a few different issues.

I would try using a regular expression to get your file name rather than split and trim. Possibly something like:

Code: Select all

 
$pic = preg_replace('@^[.]*\@', '', $url);
 
I am not fluent in regex (yet) but the idea is to replace everything before the last slash with nothing. This should be a common regular expression that's easy enough to find somewhere.

If you can do this using a database rather than a file stream then you'll be better off.

Hope that helps.
Post Reply