Failed to open stream on server error

hello…plz help me
i create a counter on my site for visitors…the code is here →

<div class="counter">
     <?php $handle = fopen("counter.txt", "r"); 
	 if(!$handle)
	 { echo "could not open the file"; }
	 
	  else 
	  { $counter=(int )fread($handle,20);
	   fclose($handle); $counter++; echo" <strong> you are visitor no : ". $counter . " </strong > " ; 
	   $handle= fopen("counter.txt", "w" ) ;
	    fwrite($handle,$counter) ; fclose ($handle) ; } ?> 
        </div>

code run on local xaamp…but when i upload on server it gives following error–>

Warning: fopen(counter.txt) [function.fopen]: failed to open stream: Permission denied in C:\Inetpub\vhosts\abhinaykatta.org\httpdocs\include\footer.php on line 197

Warning: fwrite(): supplied argument is not a valid stream resource in C:\Inetpub\vhosts\abhinaykatta.org\httpdocs\include\footer.php on line 198

Warning: fclose(): supplied argument is not a valid stream resource in C:\Inetpub\vhosts\abhinaykatta.org\httpdocs\include\footer.php on line 198

plz give me the solution

Do you need to specify the full path of the file? Perhaps the default home directory is read-only, it might be worth asking your hosting provider for clarification.

You can do an is_writable() check to see whether you have permission to write to the file or not, and you can also use fileperms() to see what the file’s permissions actually are.

Also be sure that the file exists. If you create it with PHP then it will automatically be owned by the webserver user, which makes it much easier to work with it from there. If you’ve FTP’d the file, or created it manually via SSH, then you may just need to chown the file.

Related to this, you may regret this solution if you ever get a lot of traffic. Consider using something like Memcached or Redis to manage the counter. Personally I prefer Redis. All you need to do is to tell Redis to increment a counter and it manages it for you, and can also persist to disk so that you don’t lose the counter that is in memory if you restart. There’s a nice Redis client available through Github. You’re looking for the incr command

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.