Jump to content

[SOLVED] fopen() Help


Trium918

Recommended Posts

This program is creating and writing to a file called

project4.txt, but now I am trying to read to entire file

so that it output would be some like a blog.

 

Example: Click Here!!!

 

<form method="POST">
<textarea name = "content" cols="100" rows="10">
</textarea><BR>
<input type = submit name="submit">
</form>

<?
$content = $_POST['content'];

@ $fp = fopen("project4.txt", "a");
  
  flock($fp, 2); 

  if (!$fp)
  {
    echo 'There is an error in this file!!';
    exit;
  } 

fwrite($fp, $content);
flock($fp, 3); 
fclose($fp);

?>

 

Link to comment
https://forums.phpfreaks.com/topic/43755-solved-fopen-help/
Share on other sites

My code accepts data from the user and enters

it into a text file called project4.txt, but I am trying to

get it to print output that is pulled from project4.txt.

The user is then able to post as many post as he are she

wants.

 

 

Yes I am trying to do what is in the example

Example: Click Here:

 

 

<form method="POST">
<textarea name = "content" cols="100" rows="10">
</textarea><br />
<input type = submit name="submit">
</form>

<?
$content = $_POST['content'];
$line = $_POST['line'];

@ $fp = fopen("project4.txt", "a");
  
fwrite($fp, $content);
fclose($fp);
// readfile('project4.txt');
?>

Link to comment
https://forums.phpfreaks.com/topic/43755-solved-fopen-help/#findComment-212533
Share on other sites

One way to do this would be to use the file() function. This function will read the entire file into an array, then you just have to loop through the array and output the information. Perhaps something like this:

<?php
$info = file('project4.txt');
for ($i=0;$i<count($info);$i++) {
    echo nl2br(htmlentities(stripslashes(trim($info[$i])),ENT_QUOTES));
    echo '<hr>';
}
?>

 

Ken

Link to comment
https://forums.phpfreaks.com/topic/43755-solved-fopen-help/#findComment-212542
Share on other sites

Ok, I change my code. I would like for someone to

click on the links that I provided below. The first one

is where I need to be and the second one is where I am

now. You will noticed in the first Example the line brakes

after the user click submit but Example2 doesnt. My code

is Example2.

 

Example1: Click Here

Example2: Click Here

 

<form method="POST">
<textarea name = "content" cols="100" rows="10">
</textarea><br />
<input type = submit name="submit">
</form>

<?
$content = $_POST['content'];
$handle = $_POST['handle'];

@ $fp = fopen("project4.txt", "a");
 
fwrite($fp, $content);
$handle = @fopen("project4.txt", "r");
if ($handle) {
  while (!feof($handle)) {
      $buffer = fgets($handle, 100);
      echo $buffer."<br>";
  }
  fclose($handle);
}
fclose($fp);
// readfile('project4.txt');

?>

Link to comment
https://forums.phpfreaks.com/topic/43755-solved-fopen-help/#findComment-212544
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.