/    Sign up×
Community /Pin to ProfileBookmark

simple textarea file-edit

Hi there…

I was looking (since I’m definately an early beginner in PHP) for a simple script to edit a text-file online, using a textarea. I found some php-scripts, none worked… here’s an example of what I tried:

<?php
if ($submit) {
$fp = fopen(“data.txt”, “w”);
fwrite($fp, stripslashes($newdata));
fclose($fp);
}

$fp = fopen(“data.txt”, “r”);
while (!feof($fp)) {
$data .= fgets($fp, 4096);
}
fclose($fp);
?>

<html>
<head>
<title>Edit</title>
</head>

<body>
<form action=”<? print $PHP_SELF; ?>” method=”post”>
<textarea name=”newdata” rows=”50″ cols=”100″>
<?
print $data;
?>
</textarea>
<br>
<input type=”submit” name=”submit” value=”Save”>
</form>

</body>
</html>

It didn’t return an error, but simply reloaded data.txt, doing nothing like saving the data at all. Interestingly I experienced the same with a different script. The script is placed in protected directory, together with some scripts to upload files, also written in php. Uploading works just fine. I cannot figure out why it does not work. As said: me noob *grunt*
I would gladly accept any advice or code that’ll do the job. Can’t be that complicated… Thanks.

to post a comment
PHP

6 Comments(s)

Copy linkTweet thisAlerts:
@JonaJan 18.2005 — [font=trebuchet ms]The scripts you found are based on an older version of PHP. They will cause errors, but the INI settings you have set on your server supress apparently all but fatal ones. Try the following script.[/font]

[code=php]
<?php
if(isset($_POST["submit"])){
if(@$fp = fopen("data.txt", "w")){
fwrite($fp, stripslashes($newdata));
fclose($fp);
} else {
exit ("<h1>Error</h1>n<p>Could not write to file.</p>");
}
}

if($fp = fopen("data.txt", "r")){
$data = fread($fp, filesize("data.txt"));
fclose($fp);
} else {
$data = "";
exit ("<h1>Error</h1>n<p>Could not write to file.</p>");
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Strict//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Edit</title>
</head>
<body>
<form action="<?php echo $_SERVER["PHP_SELF"]; ?>" method="post">
<fieldset>
<legend>Edit "data.txt"</legend>
<label>Data.txt contents:<br><textarea name="newdata" rows="50" cols="100"><?php
echo $data;
?></textarea></label><br>
<label><input type="submit" name="submit" value="Save"></label>
</fieldset>
</form>
</body>
</html>
[/code]
Copy linkTweet thisAlerts:
@NogDogJan 18.2005 — Might simply be a file permission problem. When you run a PHP page via the web server, it is normally run under a unique user ID, [i]not[/i] your user ID. Therefore, in order for your PHP script to update an existing file, you need to ensure that either the file is owned by the web user account, or that it has the necessary permissions for others to write to it.
Copy linkTweet thisAlerts:
@prof_frinkauthorJan 18.2005 — Thanks for your help. I tried the script, but it returned

Warning: fread(): Length parameter must be greater than 0. in /mnt/kw1/04/767/00000012/htdocs/phptest/test.php on line 12

Line 12 would be: $data = fread($fp, filesize("data.txt"));

And the script clears data.txt
Copy linkTweet thisAlerts:
@JonaJan 18.2005 — [font=trebuchet ms]If the file is opened and it is empty, that error is returned; put an "@" symbol in front of "fread" (which will yield "@fread") to supress the error. Afterwards, make sure your file's permissions are 755. If it still doesn't work, try 777.[/font]
Copy linkTweet thisAlerts:
@prof_frinkauthorJan 18.2005 — I tried, but it still does not work, even with chmod 777. I have the feeling something strange is going on in phpland...

I have a script running for uploading and replacing files and it works fine. Why should editing a file fail?
Copy linkTweet thisAlerts:
@JonaJan 18.2005 — [font=trebuchet ms]Login to your hosting account and edit the file manually, then go back to this PHP file and see if anything comes up in the textarea. If it doesn't, do you get any errors?[/font]
×

Success!

Help @prof_frink spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.16,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...