belbin09 Posted November 10, 2017 Share Posted November 10, 2017 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 28Notice: Undefined variable: cfile in /Applications/XAMPP/xamppfiles/htdocs/assignment1/enrolled.php on line 29Warning: fwrite() expects at most 3 parameters, 4 given in /Applications/XAMPP/xamppfiles/htdocs/assignment1/enrolled.php on line 29Warning: 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 Quote Link to comment https://forums.phpfreaks.com/topic/305615-writing-to-a-text-file/ Share on other sites More sharing options...
belbin09 Posted November 10, 2017 Author Share Posted November 10, 2017 I have contacted my teacher and he has given me a few suggestions I am going to check out. Quote Link to comment https://forums.phpfreaks.com/topic/305615-writing-to-a-text-file/#findComment-1553647 Share on other sites More sharing options...
requinix Posted November 10, 2017 Share Posted November 10, 2017 Warning: fopen(load.txt): failed to open stream: Permission denied in /Applications/XAMPP/xamppfiles/htdocs/assignment1/enrolled.php on line 28XAMPP 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 29As 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 29You 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 . " " . $numberand 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 30The 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. Quote Link to comment https://forums.phpfreaks.com/topic/305615-writing-to-a-text-file/#findComment-1553650 Share on other sites More sharing options...
belbin09 Posted November 10, 2017 Author Share Posted November 10, 2017 (edited) 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 November 10, 2017 by belbin09 Quote Link to comment https://forums.phpfreaks.com/topic/305615-writing-to-a-text-file/#findComment-1553652 Share on other sites More sharing options...
requinix Posted November 10, 2017 Share Posted November 10, 2017 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. Quote Link to comment https://forums.phpfreaks.com/topic/305615-writing-to-a-text-file/#findComment-1553653 Share on other sites More sharing options...
belbin09 Posted November 11, 2017 Author Share Posted November 11, 2017 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 Quote Link to comment https://forums.phpfreaks.com/topic/305615-writing-to-a-text-file/#findComment-1553666 Share on other sites More sharing options...
belbin09 Posted November 11, 2017 Author Share Posted November 11, 2017 Never mind. Found the problem and fixed it. Thank you requinix for all your help Quote Link to comment https://forums.phpfreaks.com/topic/305615-writing-to-a-text-file/#findComment-1553667 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.