tyty1993 Posted May 25, 2008 Share Posted May 25, 2008 I need some help here. I am trying to write/edit a .txt file, then try to display it on another page. I then try to display the contents of the file in a text field value which then goes wrong. It works when I use "r+" but it adds data to the file. When I use "w+" however, it doesn't display it in the text field when i call the fread() function. Here is some of the code that I am using: PHP File 1: (to write data to file) <?php $file = $_POST['file']; $data = $_POST['data']; $handling = fopen($file, 'w+'); $read = fread($handling, 1024); //Output Page include('/home/www/mywebsite.com/edit_body.html'); ?> HTML Code 1 (select a file to edit, and hit submit to write/edit data) <form method="post" action="edit.php"> <table border="0" cellspacing="0" cellpadding="2" width="100%"> <tr> <td align="left"> File to Edit </td> <td align="left"> <select name="file" id="file"> <option value="file1.txt">File 1</option> <option value="file2.txt">File 2</option> <option value="file3.txt">File 3</option> </select> </td> </tr> <tr> <td align="left"> <input type="submit" value="Submit" name="submit" id="submit"> </td> </tr> </table> </form> HTML Code 2 (edit.php/edit_body.html)(writes data to read) <form method="post" action="<?php echo $PHP_SELF; ?>"> <table border="0" cellspacing="0" cellpadding="2" width="100%"> <tr> <td align="left"> File You're Editing: </td> <td align="left"> <select name="file" id="file"> <option value="<?php echo $file; ?>"><?php echo $file; ?></option> </select> </td> </tr> <tr> <td align="left"> Data </td> <td align="left"> <input type="text" name="data" id="data" value="<?php echo $read; ?>" size="15"> </td> </tr> <tr> <td align="left"> <input type="submit" value="Submit" name="submit" id="submit"> </td> </tr> </table> </form> <?php fwrite($handling, $data); fclose($handling); ?> PHP File 2 (for read_body.html) <?php //File 1 $1 = "file1.txt"; $handle1 = fopen($1, 'r+'); $file1= fread($handle1, 1024); //File 2 $2 = "file2.txt"; $handle2 = fopen($2, 'r+'); $file2 = fread($handle2, 1024); //File 3 $3 = "file3.txt"; $handle3 = fopen($3, 'r+'); $file3 = fread($handle3, 1024); //Output Page include('/home/www/mywebsite.com/read_body.html'); ?> HTML Code 3 (reads data written, read_body.html) <table border="0" cellspacing="0" cellpadding="2" width="100%"> <tr> <td align="center"> <b>File</b> </td> <td align="center"> <b>Content</b> </td> </tr> <tr> <td align="center"> <?php echo $1; ?> </td> <td align="center"> <?php fclose($handle1); echo $file1; ?> </td> </tr> <tr> <td align="center"> <?php echo $2; ?> </td> <td align="center"> <?php fclose($handle2); echo $file2; ?> </td> </tr> <tr> <td align="center"> <?php echo $3; ?> </td> <td align="center"> <?php fclose($handle3); echo $file3; ?> </td> </tr> </table> Yeah, I know it's very complicated, but i need someone's help. Most of the HTML files are included within the php file. Can anyone help me? Link to comment https://forums.phpfreaks.com/topic/107226-fwrite-fopen-fread-writing-to-a-file-and-reading-it-on-a-page/ Share on other sites More sharing options...
tyty1993 Posted May 31, 2008 Author Share Posted May 31, 2008 *bump* Link to comment https://forums.phpfreaks.com/topic/107226-fwrite-fopen-fread-writing-to-a-file-and-reading-it-on-a-page/#findComment-554409 Share on other sites More sharing options...
unidox Posted May 31, 2008 Share Posted May 31, 2008 Please clarify your question. And w+ isint the same as r+, so I dont understand why you would use w+ and fread together. Link to comment https://forums.phpfreaks.com/topic/107226-fwrite-fopen-fread-writing-to-a-file-and-reading-it-on-a-page/#findComment-554445 Share on other sites More sharing options...
tyty1993 Posted June 1, 2008 Author Share Posted June 1, 2008 Well, the fread() is used to read the data that was written, as somewhat shown here... HTML Code 3 (reads data written, read_body.html) <table border="0" cellspacing="0" cellpadding="2" width="100%"> <tr> <td align="center"> <b>File</b> </td> <td align="center"> <b>Content</b> </td> </tr> <tr> <td align="center"> <?php echo $1; ?> </td> <td align="center"> <?php fclose($handle1); echo $file1; ?> </td> </tr> <tr> <td align="center"> <?php echo $2; ?> </td> <td align="center"> <?php fclose($handle2); echo $file2; ?> </td> </tr> <tr> <td align="center"> <?php echo $3; ?> </td> <td align="center"> <?php fclose($handle3); echo $file3; ?> </td> </tr> </table> So, I use one page (PHP Code 1/HTML Code 1) to write to/edit a text file, and read what the data was written from the text file that was just written. (PHP Code 2/HTML Code 3). doesn't w+ means writing and reading? Link to comment https://forums.phpfreaks.com/topic/107226-fwrite-fopen-fread-writing-to-a-file-and-reading-it-on-a-page/#findComment-554951 Share on other sites More sharing options...
unidox Posted June 1, 2008 Share Posted June 1, 2008 I know the fread() function.... But you said you were using fopen with w+, if your going to use fread, you need r+. If your also going to write to a file, open the file again. Link to comment https://forums.phpfreaks.com/topic/107226-fwrite-fopen-fread-writing-to-a-file-and-reading-it-on-a-page/#findComment-554964 Share on other sites More sharing options...
BlueSkyIS Posted June 1, 2008 Share Posted June 1, 2008 doesn't w+ means writing and reading? correct. // Open file stream $fileStream = fopen('myFile.txt', 'w+'); // Write to our file fwrite($fileStream, "Hello World!"); // Read the file $fileContainer = fread($fileStream, filesize('myFile.txt')); // Echo the content of myFile.txt echo $fileContainer; Link to comment https://forums.phpfreaks.com/topic/107226-fwrite-fopen-fread-writing-to-a-file-and-reading-it-on-a-page/#findComment-554970 Share on other sites More sharing options...
tyty1993 Posted June 2, 2008 Author Share Posted June 2, 2008 There is an error... Warning: fread() [function.fread]: Length parameter must be greater than 0. in /home/www/mywebsite.com/edit.php on line 6 Link to comment https://forums.phpfreaks.com/topic/107226-fwrite-fopen-fread-writing-to-a-file-and-reading-it-on-a-page/#findComment-555239 Share on other sites More sharing options...
haku Posted June 2, 2008 Share Posted June 2, 2008 If you don't show us line 6 (and possible some of the code before that), then we can't really tell you why you are getting the error, can we. Link to comment https://forums.phpfreaks.com/topic/107226-fwrite-fopen-fread-writing-to-a-file-and-reading-it-on-a-page/#findComment-555245 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.