p2p.wrox.com Forums

Need to download code?

View our list of code downloads.


  Return to Index  

beginning_php thread: Errors trying to process Online Poll Data


Message #1 by "Peter Riley Osbonre" <peter@g...> on Wed, 26 Feb 2003 06:22:08
Hey, I keep getting this error:

Warning: fopen("poll.txt", "w+") - Permission denied 
in /home2/careen/careen-www/poll.php on line 129

Warning: fputs(): supplied argument is not a valid File-Handle resource 
in /home2/careen/careen-www/poll.php on line 130

Warning: fclose(): supplied argument is not a valid File-Handle resource 
in /home2/careen/careen-www/poll.php on line 131


Whenever I try and run my poll.  If I change the the fopen call to 
include "http://www.blah.com/poll.txt" it will remove the error, but I 
still cannot record the new data, and I end up getting the same number of 
results.  Here is the code I am working with:

<?
  if ($poll)
  {

  include("poll.txt");



  if ($poll=="starwars"){$poll_count[0]=$poll_count[0]+1;}


  if ($poll=="empire"){$poll_count[1]=$poll_count[1] + 1;}


  if ($poll=="jedi"){$poll_count[2]=$poll_count[2]+1;}

  if ($poll=="phantom"){$poll_count[3]=$poll_count[3]+1;}
  $total=$poll_count[0]+$poll_count[1]+$poll_count[2]+$poll_count[3];

	$poll0 = (doubleval($poll_count[0])/doubleval($total))*100;
    $poll1 = (doubleval($poll_count[1])/doubleval($total))*100;
    $poll2 = (doubleval($poll_count[2])/doubleval($total))*100;
    $poll3 = (doubleval($poll_count[3])/doubleval($total))*100;

	echo("<p class=main>");
    echo("<BR>");
    echo ("Total Votes:" . $total);

    echo("<BR>");
    echo("Star Wars: ". intval($poll0) ."%<br><img 
src=img/spotbackground.gif width=" . intval($poll0 * 3) ." height=8>");
    echo("<BR><BR>");
    echo("Empire Strikes Back: ". intval($poll1) ."%<br><img 
src=img/spotbackground.gif width=" . intval($poll1 * 3) ." height=8>");
    echo("<BR><BR>");
    echo("Return of the Jedi: ". intval($poll2) ."%<br><img 
src=img/spotbackground.gif width=" . intval($poll2 * 3) ." height=8>");
    echo("<BR><BR>");
    echo("Phantom Menace: ". intval($poll3) ."%<br><img 
src=img/spotbackground.gif width=" . intval($poll3 * 3) ." height=8>");
  	echo("<BR><BR>");



if(!$poll_count[0]){$poll_count[0]="0";}
if(!$poll_count[1]){$poll_count[0]="0";}
if(!$poll_count[2]){$poll_count[0]="0";}
if(!$poll_count[3]){$poll_count[0]="0";}

  // Create output string


$output="<? \$poll_count[0]=".$poll_count[0].";\n \$poll_count[1]
=".$poll_count[1]."; \n \$poll_count[2]=".$poll_count[2]."; \n \$poll_count
[3]=".$poll_count[3].";?>";





// open file and output to it
$file=fopen("poll.txt","w+");
fputs($file,$output);
fclose($file);
}


?>

Any help would be great.  Thanks!

Peter
Message #2 by "Nikolai Devereaux" <yomama@u...> on Wed, 26 Feb 2003 09:25:08 -0800
> Hey, I keep getting this error:
>
> Warning: fopen("poll.txt", "w+") - Permission denied
> in /home2/careen/careen-www/poll.php on line 129
>
> Warning: fputs(): supplied argument is not a valid File-Handle resource
> in /home2/careen/careen-www/poll.php on line 130
>
> Warning: fclose(): supplied argument is not a valid File-Handle resource
> in /home2/careen/careen-www/poll.php on line 131


Well, the error seems obvious -- PHP (read: your webserver user) doesn't
have permission to write to poll.txt, so fopen() fails.  Each successive
file call that accepts the file handle returned by fopen() complains that
the handle isn't valid (since fopen didn't succeed).

Double check your file and directory permissions.


> Whenever I try and run my poll.  If I change the the fopen call to
> include "http://www.blah.com/poll.txt" it will remove the error, but I
> still cannot record the new data, and I end up getting the same number of
> results.

Changing the filename to a http url means that you're opening an HTTP
connection.  You can't write to that file any more than I can write to it
from here.

It's written all over the manual that you cannot write to remote files.

Fix your permissions, and your problems will go away!


Take care,

Nik


  Return to Index