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.

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....

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

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.