View Full Version : Is it possible to edit a text file via PHP?
rctxtreme
04-06-2007, 01:55 AM
I think it is, because I use SMF and when you edit its settings, /settings.php gets changed.
But how do you do this?
thetestingsite
04-06-2007, 02:05 AM
You would want to use the file system functions in PHP. fopen (http://www.php.net/fopen), fread (http://www.php.net/fread), fwrite (http://www.php.net/fwrite), file_get_contents (http://www.php.net/file_get_contents), and some other ones that you can see for yourself on their website (PHP.net).
A simple example of how to write to a text file:
<?php
$filename = 'test.txt';
$string = "This is some text that will be written to the test file!";
$fp = fopen($filename, 'w');
fwrite($fp, $string);
fclose($fp);
?>
Hope this helps.
mburt
04-06-2007, 03:07 AM
In the fopen call, doesn't the handle have to be a string?
$fp = fopen($filename, "w");
thetestingsite
04-06-2007, 03:14 AM
Ah yes, typing fast and missed my quotes; although, this will probably still work. Anyways, thanks for pointing that out Mike.
It will still work without quotes (unless there's a define with the name w) but it shouldn't be relied upon, because it requires error-correction to work and would conflict with the aforementioned define if it were introduced.
motormichael12
04-09-2007, 06:07 PM
i have a script on my home computer that makes a text box containing the text file, you can edit it and click save and it makes the whole file look like the text box did.... i will post it later
mburt
04-09-2007, 08:00 PM
I think the issue is simply writing to a text file, not the whole she-bang. :p
thetestingsite has explained what to do, anyways.
motormichael12
04-12-2007, 09:41 PM
<?php
if ($changefile) {
$slash = stripslashes($_POST['filetest']);
$filetochange = "http://site.com/file.txt";
$filetochangeOpen = fopen($filetochange,"w") or die ("Error editing.");
fputs($filetochangeOpen,$slash);
fclose($filetochangeOpen) or die ("Error Closing File!");
}
?>
<form method=post action="">
<textarea rows="40" cols="60" name="filetest">
<?
// Implode CSS
$filetochange = "http://site.com/file.txt";
print (implode("",file($filetochange)));
?>
</textarea><br />
<br />
<input type="submit" value="Change File" name="changefile">
</form>
Znupi
04-14-2007, 08:57 PM
1. You should use implode("\r\n", $filetochange);
2. You can dump the whole print(implode("\r\n", $filetochange)); for print(file_get_contents($filetochange));
1. You should use implode("\r\n", $filetochange);You probably mean implode("\r\n", file($filetochange)), but the \r\n is only for Windows: UNIX-based operating systems use just \n, and the old Mac-based operating systems (pre OS X) use just \r.
2. You can dump the whole print(implode("\r\n", $filetochange)); for print(file_get_contents($filetochange));Only in PHP 4.3.0 and later.
andersmoen
04-15-2007, 11:29 AM
Yes, you can. This is really simple.
The file you want to write to: (call it something.txt)
Here it just says some random words, and yeah... this is just for filling out some, hehe.
The file where you write something new to that file:
Over the doctype:
<?php
if (isset($_POST['submit'])) {
$myFile = "something.txt";
$fh = fopen($myFile, 'w') or die("can't open file");
$stringData = stripslashes($_POST['sf']);
fwrite($fh, $stringData);
fclose($fh);
header('Location: the_file_where_you_want_to_edit_the_textfile.php?a=done');
}
?>
Inside body:
<form action="" method="post">
<textarea name="sf" cols="40" rows="6">
<?php
$myFile = "something.txt";
$fh = fopen($myFile, 'r');
$theData = fgets($fh);
fclose($fh);
echo $theData;
?></textarea>
<br />
<input type="submit" name="submit" value="Edit" />
</form>
<?php
if ($_GET['a'] == 'done') {
echo 'The file was saved and now it says:<br /><br />';
$myFile = "something.txt";
$fh = fopen($myFile, 'r');
$theData = fgets($fh);
fclose($fh);
echo $theData;
}
?>
Remember to change the CHMOD for the txt file to 777 - or don't you have to do that on txt-files?
Tell me if it doesn't work ;)
mburt
04-15-2007, 11:40 AM
I don't understand why you're redirecting to "the_file_where_you_want_to_edit_the_textfile", seeings you are on the file that's editing the textfile.
It should work though, but this is nothing that hasn't been mentioned before.
munnabhaiec34
07-15-2010, 01:19 PM
I think it is, because I use SMF and when you edit its settings, /settings.php gets changed.
But how do you do this?
Is it possible to edit a text file via PHP?
fileserverdirect
07-15-2010, 05:02 PM
http://php.net/manual/en/function.file-put-contents.php
Make sure the file you are editing is CHMOD'ed to 777!
djr33
07-15-2010, 05:53 PM
Please start a NEW discussion for a NEW question. I'm going to close this. If you need more help, start a new thread and please don't post in a 3-year-old thread unless it is a direct response to the topic.
Powered by vBulletin® Version 4.2.2 Copyright © 2021 vBulletin Solutions, Inc. All rights reserved.