Reputation: 39
I'm a real noob with PHP, so here' goes.
I have a script to edit a file with a form in it for what to put in the file, but every time I go to the page, it runs the script and clears the file before I put it in.
Also, how do I get it to submit to other pages, not just the page I did?
<?php
$ourFileName = $_GET['page'];
$ourFileHandle = fopen($ourFileName, 'w') or die("can't open file");
$stringData = $_POST['stuff'];
fwrite($ourFileHandle, $stringData);
fclose($ourFileHandle);
?>
<form action="edit.php?page=index.html" method="post">
<textarea name="stuff" rows="10" cols="50" wrap="virtual" maxlength="300"></textarea>
<input name="submit" type="submit" value="Submit">
</form>
Upvotes: 2
Views: 183
Reputation: 490203
You want the a+
flag for your second argument.
Open for reading and writing; place the file pointer at the end of the file. If the file does not exist, attempt to create it.
Also, using a GET param to open a file leaves you open to all sorts of vulnerabilities, such as directory traversal attacks, and writing to your existing PHP files (which in turn could let a malicious user do anything PHP can do).
You should also make sure those variables are set before accessing them (and therefore opening the file and writing needlessly).
if (isset($_GET['page']) AND isset($_POST['stuff'])) { ... }
To submit to other pages, change your form
's action
attribute to point to the new page.
Upvotes: 2
Reputation: 1068
Put the file managing code inside an if (isset $_POST['stuff']) {...}
Upvotes: 0