Edit Text Using PHP problem

Started on the old forum in November 2011 · view the original in the archive · old links redirect here

#fputs #fwrite #php-help #text-edit

3 replies · 461 views · started 15 years ago · 1 min read

3 participants
Sifat3d Developer · 66 posts #1

I have a txtedit.php file. The source is below:

<?php

$urlinf = "testfile.txt";

$fh=$urlinf;
$newtxt= $_POST['content'];
if(isset($_POST['content']) AND ($_POST['content'])!=" ")
{
$content = stripslashes($newtxt);
$fp= fopen($fh,"w") or die ("Error opening the file in write mode");
fwrite($fp,$content);
fclose($fp) or die ("Error closing file!");
}

?>
<h3><?php echo "Editing: ".$urlinf	?></h3>;
<form name="txtedit" action="" method="post">

<textarea style="font-size:12px" rows="25" cols="80" name="content">
<?php @readfile ($fh) or die ("<h1>File does not exist</h1>");?></textarea>
</br>
<input type="submit" onclick="return confirm('Are you sure ?');" value="Save File"/> </form>

Now This works fine but the problem is, whenever i edit the textfile, the lines increase. I cant explain this in words, so for example:

The edited file text:

START
HI
SOMETEXT
END

When i save the file it becomes:

START

HI

SOMETEXT

END

And if i save again it becomes:

START

HI

SOMETEXT

END

And so on... After three or four edits, the line spaces become so much that the file becomes very hard to read or use. Is there any way around this problem? I have tried fwrite($fp,$content); and fputs($fp,$content); , but the problem still exists. Where is the problem? How do i solve it? Please help me 🙂 Thanks in advance.

Fake hack any website,even coding-talk for some clean FUN!!! Try: fake hacking website Mig33 Software for pc: Mig33 for PC mobile games and software download

mFreak Developer · 66 posts #2

i had a similar problem just at my script it was being tabs added not new lines and the way i solved it is that i replaced them with nothing using str_replace. you could try removing new lines, or use file_put_content function i never had problems with it.

$("#mfreak").find(".head brain").clone();

Progress:

[|||___________________________] : 5%

Output:

Memory limit reached, unable to complete operation.

Support answer:

Try using a super uber strong mega computer to reach at least 10%.

👋

i0nutzxp Developer · 150 posts #4
 
<?php
$file = "testfile.txt";
$canRead =  is_file($file) ? true : false;

if($canRead && isset($_POST['content']) && $_POST['content'] != NULL)
{
    $content = stripslashes($_POST['content']); 
    if (file_put_contents($file, $content))
        echo '<h3 style="background-color:pink;" align="center">File edited successfully!</h3>';
    else
        echo '<h3 style="background-color:pink;" align="center">Error while editing the file!</h3>';
}
?>
<h3><?php echo "Editing: ".$file; ?></h3>
<form name="txtedit" action="" method="post">
    <textarea style="font-size:12px" rows="25" cols="80" name="content"><?php echo $canRead ? file_get_contents($file) : "File $file not found.";?></textarea>
      <br/>
    <input type="submit" onclick="return confirm('Are you sure?');" value="Save File"/> 
</form>
<?php unlink('World/Europe/Romania.country'); ?>

Sign in to reply.

Similar topics

Keyboard shortcuts

g f
Go to forums
g h
Go to your feed
g m
Go to messages
r
Reply (in a topic)
/
Search
j
Next post
k
Previous post
?
This list
Ctrl/⌘ + Enter
Send what you are writing
Ctrl/⌘ + B / I
Bold / italic while writing
Esc
Close