Jump to content

Recommended Posts

So I am trying to write to a text file that I can then display on enrolled.php with the students name, number and courses selected. Then when a different student is selected it writes over the file. Based on the examples I have seen online and what my teacher has shown us I have tried writing the code for this. However I keep getting these errors. Do I have to implode in order to write to the txt file? Sorry this is the first time I have written in php.

 

Warning: fopen(load.txt): failed to open stream: Permission denied in /Applications/XAMPP/xamppfiles/htdocs/assignment1/enrolled.php on line 28

Notice: Undefined variable: cfile in /Applications/XAMPP/xamppfiles/htdocs/assignment1/enrolled.php on line 29

Warning: fwrite() expects at most 3 parameters, 4 given in /Applications/XAMPP/xamppfiles/htdocs/assignment1/enrolled.php on line 29

Warning: fclose() expects parameter 1 to be resource, boolean given in /Applications/XAMPP/xamppfiles/htdocs/assignment1/enrolled.php on line 30

 

index.php

<!DOCTYPE html>
<html>
  <body>
    <h1>Course Selection</h1><br>


    <form action="enrolled.php" method="post">


              Name: <input type="text" name="name" placeholder="Name" required="required" maxlength="50">
              <br><br>

              Student Number: <input type="text" name= "snumber" required="required" maxlength="9">
              <br><br>

        <?php
        //form
      
      $coursedata = "course.txt"; // text file coursedata
      echo "Select a course: <select name = \"pcourse\">\n";

      $cfile = fopen ($coursedata, 'r+') or die ("File does not exist or you do not have permission");

      while ($line = fgets ($cfile)) {
        $drop = explode ('||', $line);

      echo " <option value =\"$drop[0]\">$drop[0] $drop[1] $drop[2]</option>\n";


      }

      fclose ($cfile);

      echo " </select>\n";

      }

      ?>

      <br><br>
      <input type = "submit" value="submit" name= "submit">

    </form>

    </body>
    </html>

enrolled.php

 

  <?php
 
  $students = "student.txt"; // text file for students and student number
 
  //converting a string into a variable
 
  $name = $_POST["name"];
  $number = $_POST["snumber"];
 
 
//open student file and explode into an array
 
  $sfile = fopen($students, 'r') or die ("Student file does not exist");
 
  $found = 0;
  while ($sline = fgets ($sfile)) {
    $list = explode(",",trim ($sline));
 
    //test array against text input
 
    if ($name == $list[0] && $number == $list[1]) {
 
      $found = 1;
 
      //load name, number and course selected into load.txt
 
      $handle = fopen ('load.txt', 'a');
      fwrite ($handle, $name, $number, $cfile."\n");
      fclose ($handle);
 
      $readin = file ('load.txt');
      break;
    }
 
  } // end of while
 
  if (!$found) {
 
    include 'index.php';
  }
 
  fclose($sfile)
 
  ?>
 

course.txt

student.txt

Link to comment
https://forums.phpfreaks.com/topic/305615-writing-to-a-text-file/
Share on other sites

Warning: fopen(load.txt): failed to open stream: Permission denied in /Applications/XAMPP/xamppfiles/htdocs/assignment1/enrolled.php on line 28

XAMPP and PHP need permissions from your Mac to be able to (likely) create that file.

 

Make it yourself first, then change its permissions so anyone can write to it.

 

Notice: Undefined variable: cfile in /Applications/XAMPP/xamppfiles/htdocs/assignment1/enrolled.php on line 29

As the message says, the variable is not defined.

 

Warning: fwrite() expects at most 3 parameters, 4 given in /Applications/XAMPP/xamppfiles/htdocs/assignment1/enrolled.php on line 29

You aren't using fwrite() correctly. The basic usage is

fwrite($file_handle, $value_to_write);
If you want to write both the $name and $number (and "$cfile"?) then combine them together into one value, like with

$name . " " . $number
and use it as the second argument.

 

Warning: fclose() expects parameter 1 to be resource, boolean given in /Applications/XAMPP/xamppfiles/htdocs/assignment1/enrolled.php on line 30

The first parameter was $handle, and in this case it being a boolean value means the file could not be opened. Which you already know. Make sure the file can be opened and this error will go away.

 

Additionally,

$readin = file ('load.txt');
Why are you doing this? You don't use $readin.

Thank you requinix.

 

I do have a question about passing arrays. So in index.php I open the course.txt file and create an array. Now in enrolled.php I want to call that array so that I can put it into the fwrite parameters. How would I do that?

 

 

index.php

$coursedata = "course.txt"; // text file coursedata
      echo "Select a course: <select name = \"pcourse\">\n";

      $cfile = fopen ($coursedata, 'r+') or die ("File does not exist or you do not have permission");

      while ($line = fgets ($cfile)) {
        $drop = explode ('||', $line);

      echo " <option value =\"$drop[0]\">$drop[0] $drop[1] $drop[2]</option>\n";


      }

      fclose ($cfile);

      echo " </select>\n";

enrolled.php

  $handle = fopen ('load.txt', 'a');
      fwrite($handle, "$number || $drop\n");
      fclose ($handle);

      break;
    }
Edited by belbin09

Your form is only sending the first value, $drop[0]. If you want the rest then read course.txt, much like you did in index.php, to find the line with the matching first value.

 

Sending all parts of the line is possible, but a Best Practice for this sort of thing is to only send one unique value ($drop[0]) and have the receiving script look up the rest on its own. It's a security issue.

OK so I added in the course.txt file and everything is printing and displaying, however I keep getting this error and I am not sure what it means

 

Warning: fgets(): 3 is not a valid stream resource in /var/www/htdocs/home/bethany/enrolled.php on line 15

$coursedata = "course.txt"; // text file coursedata
  $cfile = fopen ($coursedata, 'r');

  while ($line = fgets ($cfile)) {
    $drop = explode ('||', trim ($line));

    fclose ($cfile);

  }
//open student file and explode into an array

  $sfile = fopen($students, 'r') or die ("Student file does not exist");

  $found = 0;
  while ($sline = fgets ($sfile)) {
    $list = explode(",",trim ($sline));

    //test array against text input

    if ($name == $list[0] && $number == $list[1]) {

      $found = 1;

      //load name, number and course selected into load.txt;

      $handle = fopen ('load.txt', 'a');
      fwrite($handle, "$number, $drop[1]\n");
      fclose ($handle);

      include 'load.txt';

      break;
    }

  } // end of while
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.