I think it is, because I use SMF and when you edit its settings, /settings.php gets changed.
But how do you do this?
Printable View
I think it is, because I use SMF and when you edit its settings, /settings.php gets changed.
But how do you do this?
You would want to use the file system functions in PHP. fopen, fread, fwrite, 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:
Hope this helps.Code:<?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);
?>
In the fopen call, doesn't the handle have to be a string?
Code:$fp = fopen($filename, "w");
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.
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
I think the issue is simply writing to a text file, not the whole she-bang. :p
thetestingsite has explained what to do, anyways.
Code:<?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>
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));
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.Quote:
1. You should use implode("\r\n", $filetochange);
Only in PHP 4.3.0 and later.Quote:
2. You can dump the whole print(implode("\r\n", $filetochange)); for print(file_get_contents($filetochange));
Yes, you can. This is really simple.
The file you want to write to: (call it something.txt)
The file where you write something new to that file:Code:Here it just says some random words, and yeah... this is just for filling out some, hehe.
Code: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 ;)
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.
http://php.net/manual/en/function.file-put-contents.php
Make sure the file you are editing is CHMOD'ed to 777!
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.