Jump to content

[SOLVED] getting file contents


witakr

Recommended Posts

im adding to the script that which is not included on this script so far a requirement for the file to be created before it can be written to...

 

I can set it to 0 because thats what the error is... the error is saying that it need to be greater than 0, hence the reason i used 1 and then change it to 0 after reading it.

Link to comment
Share on other sites

Are you sure? Does not the error say, file size is zero?

 

Zero is a character and and the zize of it is more than 0 kb, no?

 

But if 0 does not work, put the word zero so you do not get conflict with score = 1, unless there is no such a score, but good programming would be word zero....so the script is portable....

Link to comment
Share on other sites

this is the error:

 

 

 

Warning: fread() [function.fread]: Length parameter must be greater than 0 in /home/witakr/public_html/scorekeeper/processpoints.php on line 37

 

 

there is no possible way to score 0 unless you dont play

Link to comment
Share on other sites

there isnt a problem anymore because I required the file be created before it can be written to and durring the file creation the file has "no score" written to it so that whe n fread() reads it there will be a value great enough to avoid the error...

 

heres my code:

==============================================

index.php

==============================================

<html>
  <head>
    <title>Score Keeper</title>
    <meta http-equiv="content-type" content="text/html; charset=ISO-8859-1">
    <meta name="generator" content="syn">
  </head>
  <body>
  
  <?php 
$sitepath = "http://www.YOURSITE.com/"; //not currently used
$scriptdir = "scorekeeper/";
$filesdir = "playerfiles/";
?>
<br><BR><BR>
<center>
<table width="300" style="border: 5px double #000000">
  <tr><td colspan=2 align=center> Scores </t></tr>

<?php

$files = glob("playerfiles/*.txt");

foreach ($files AS $file)
{
$filecon = file_get_contents($file);
$name = basename($file, '.txt');
$cname = ucwords($name);

   echo "<tr><td width='75%' align='left' style='border: 1px dashed #000000'>".$cname."</td><td width='25' align='right' style='border: 1px dashed #000000'>".$filecon."</td></tr>";
}

?>
</table>
<form name='Score Keeper' action='processpoints.php' method='Post' enctype='multipart/form-data' onsubmit='return verifyMe();'>
<table class='table_form_1' id='table_form_1' cellspacing='0'>
<tr>
<td class='ftbl_row_1' ><LABEL for='playername'>Player Name:
</td>
<td class='ftbl_row_1a' >
<SELECT style='border: 1px solid #000000; background: #ffffff;' name='playername' id='playername'>  <OPTION>-----SELECT PLAYER-----
<?php
$files = glob("playerfiles/*.txt");

foreach ($files AS $file)
{
$name = basename($file, '.txt');
$cname = ucwords($name);
       echo "<OPTION>".$cname; }
?>
</SELECT>
</td>
</tr>
<tr>
<td class='ftbl_row_2' ><LABEL for='addpoints'>Add Points:
</td>
<td class='ftbl_row_2a' ><input style='border: 1px solid #000000; background: #ffffff;' type='text' name='addpoints' id='addpoints' size='25' value=''>
</td>
</tr>
<tr>
<td colspan='2' align='right'><input style='border: 1px solid #000000; background: #ffffff;' type='submit' name='submit' value='Submit'> <input style='border: 1px solid #000000; background: #ffffff;' type='reset' name='reset' value='Reset'>
</td>
</tr>
</table>    </form>
<BR><hr><br>
<form method="post" action="addplayer.php" target="_self" name="addplayer"><table dir="ltr" class="">
<tr>
	<td align="right"> </td>
	<td>Enter name to add player:<br><input style='border: 1px solid #000000; background: #ffffff;' name='playername' id='playername' type="text" name="playername" size="45" value="" maxlength="45"></td>
	</tr>
<tr>
	<td align="right"></td>
	<td><input style='border: 1px solid #000000; background: #ffffff;' name='playername' id='playername' value='Add Player' type="submit" name="addplayersubmit"></td>
	</tr>

</table></form>

</center>
  </body>
</html>

 

==============================================

processpoints.php

==============================================


<html>
  <head>
    <title>Points processor...</title>
    <meta http-equiv="content-type" content="text/html; charset=ISO-8859-1">
    <meta name="generator" content="syn">
  </head>
  <body>
                                               <center>
<?php



      $sitepath = "http://www..YOURSITE.com/"; //not currently used
      $scriptdir = "scorekeeper/";
      $filesdir = "playerfiles/";
      $addpoints = $_POST["addpoints"];
      $playername = $_POST["playername"];
      $pnlower = strtolower($playername);
      $filename = $pnlower.".txt";

  if( !$playername ) { echo "You need to enter a playername<br><a href='index.php'>[ Go Back ]</a>";
   exit; }
  if( $playername == "-----SELECT PLAYER-----" ) { echo "You need to enter a playername<br><a href='index.php'>[ Go Back ]</a>";
   exit; }
  if( $addpoints <= 0 or !$addpoints ) { echo "You need to enter a points value that is greater than zero.<br><a href='index.php'>[ Go Back ]</a>";
   exit; }


$handle = fopen("playerfiles/$filename", "r");  //opens same file for reading

$old_score = fread($handle, filesize("playerfiles/$filename")); //read file and save to var
             fclose($handle);
             
  if ($old_score == "no score") { $old_score = "0"; }  //if the var is set to 1(new file) then set it to 0 then move on

$add_score = $addpoints;
$new_score = $old_score + $add_score;

$handle = fopen("playerfiles/$filename", "w");
          fwrite($handle, $new_score);
          fclose($handle);
echo "<b>";
echo "Score updated for ".$playername."<br>";
echo "Old score: ".$old_score."<br>";
echo "Added this: ".$add_score."<br>";
echo "New Score: ".$new_score."<br>";
echo $old_score." + ".$add_score." = ".$new_score."<br>";
echo "<a href='index.php'>[ Go Back ]</a>";
echo "</b>";
?>                                                     </center>



  </body>
</html>

 

==============================================

addplayer.php

==============================================

<html>
  <head>
    <title>Untitled</title>
    <meta http-equiv="content-type" content="text/html; charset=ISO-8859-1">
    <meta name="generator" content="syn">
  </head>
  <body>

<?php
      $playername = $_POST["playername"];
      if (!$playername or $playername == "Add Player") { echo "you need to enter a name, <a href='index.php'>go back</a> and choose a name.";
           exit;}else {
      $pnlower = strtolower($playername);
      $filename = $pnlower.".txt";
      if (file_exists("playerfiles/$filename")) { echo "name already in use <a href='index.php'>go back</a> and choose a different name.";
           exit;}else {
    $handle = fopen("playerfiles/$filename", "w");
          fwrite($handle, "no score");
          fclose($handle);
          echo "player added: ".$playername." <a href='index.php'>go back</a>";} }
?>

  </body>
</html>

==============================================

 

this script will work if you save them with the correct file names and you chmod a folder called "playerfiles" to 777

Link to comment
Share on other sites

I am unable to mark this thread solved but it is so if there is a moderator who would like to mark it solved then you should know that there is no longer a problem and that marking it solved for me would be great, thanks

 

Thanks again to everyone who helped me and gave me awesome suggestions... they truly are appreciated...

 

 

 

j

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.