Jump to content

File handling problem


nvidia

Recommended Posts

Hi i have a form where user selects radio button  values. After pressing submit, these values are then written to a text file called C2.txt which is stored directly under my C: drive. The problem i have is trying to write these values to my text file. When they press submit, the % of user clicks for a value should be displayed based on my txt file, but it does not.

handler1.php

<html>
<head>
  <title> Page 2 </title>
</head>
<body>
<?php
  session_start();
$selection = $_POST['select'];
$filename = "C:\C2.txt";


function file_put_contents($filename,$selection)
    {
      $fp = fopen($filename,'a+');
      if(!$fp)
       {
          return false;
       }
else
       {
      $write = fwrite($fp, "$selection\r\n");
      fclose($fp);
       return true;
        }
    }
  $put = file_put_contents($filename, $selection);
  $contents = file_get_contents("C2.txt");

  $Hugh_Jackman  = "Hugh Jackman";
  $Tom_Cruise = "Tom Cruise";
  $Halle_Berry = "Halle Berry";

  $countHugh = substr_count($contents,"$Hugh_Jackman ");
  $countTom = substr_count($contents,"$Tom_Cruise");
  $countHalle = substr_count($contents,"$Halle_Berry");

?>

<br />
   <? echo $countHugh ?> % chose Hugh Jackman to do a bush tucker trial
  <br />
   <? echo $countTom ?> % chose Tom Cruise to do a bush tucker trial
  </br>
   <? echo $countHalle ?> % chose Halle Berry to do a bush tucker trial

</body>
</html>

 

Can somone tell me why it is not writting to my file please.

Link to comment
https://forums.phpfreaks.com/topic/41014-file-handling-problem/
Share on other sites

Sorry, I just noticed that's the name of your function. Ignore last reply.

 

PHP5 has a file_put_contents() function and I thought you were calling that one (I assume you are using php4)

which would write the latest $selection only to the file.

 

try

 

 

$filename = "C:\\C2.txt";

 

...

 

 

$contents = file_get_contents("C:\\C2.txt");

Link to comment
https://forums.phpfreaks.com/topic/41014-file-handling-problem/#findComment-198674
Share on other sites

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.