Jump to content

Php edit file


Linjon

Recommended Posts

So i made new topic as some people wanted :)

I have this script and i have file called "oldfile.php". There is variable $age = "0" but i want to change that via form. How to make that?

<html>
<body>
<form method="post">
<table>
<tr><td>Age:</td><td><input type="text" name="age"></td>	</tr>

<tr><td><input type="submit" value="Submit" name="submit"></td></tr>
</table>
</form>
</body>
</html>
if (isset($_POST['submit']))
{
$myFile = "oldfile.php";
$fh = fopen($myFile, 'w') or die("can't open file");
$age = $_POST['age'];
fwrite($fh, $access);
fclose($fh);
echo "Done!";
}
?>

Link to comment
Share on other sites

What are you trying to do? Are you trying to change the statement '$age = 0' in a php file to the value of age passed in the form? If so you will need to use string literals to built the query to search for ie '$age = ' and the post value to built the replace ie '$age = ' . $_POST['age']; and then search and replace it in the file and then rewite the new code in the file.

 

$fileContents = file_get_contents('oldfile.php');

$newFile = str_replace('$age = 0;', '$age = ' . $_POST['age'], $fileContents);

file_put_contents('oldfile.php', $newFile);

Link to comment
Share on other sites

What are you trying to do? Are you trying to change the statement '$age = 0' in a php file to the value of age passed in the form?

Exactly! :) Thanks for code. How to make it with two variable?

 

changevariable.php:

<html>
<body>
<form method="post">
<table>
<tr><td>Name:</td><td><input type="text" name="name"></td>	</tr>
<tr><td>Age:</td><td><input type="text" name="age"></td>	</tr>

<tr><td><input type="submit" value="Submit" name="submit"></td></tr>
</table>
</form>
</body>
</html>

<?php
$defaultname = "nothing";
$defaultage = "0";
$age = $_POST['age'];
$name = $_POST['name'];

$fileContents = file_get_contents('vanafail.php');
$newFile = str_replace($defaultname, $name, $fileContents);
$newFile = str_replace($defaultage, $age, $fileContents);
file_put_contents('oldfile.php', $newFile);

?>

 

oldfile.php:

<?php
$defaultname = "nothing";
$defaultage = "2";
?>

 

But that changes only AGE :/

Link to comment
Share on other sites

Can I assume that this is just a mental exercise? That you aren't trying to use this in a real-world application?

 

$defaultname = "nobody";
$defaultage = 0;
include "oldfile.php";

$name = (empty($_POST["name"]) ? $defaultname : (string)$_POST["name"]);
$age = (empty($_POST["age"]) || !ctype_digit($_POST["age"]) ? $defaultage : (int)$_POST["age"]);

$name = addslashes($name);
file_put_contents("oldfile.php", \$defaultname = "$name";
\$defaultage = $age;
?>
NEWFILE
);

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.