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
https://forums.phpfreaks.com/topic/239931-php-edit-file/
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
https://forums.phpfreaks.com/topic/239931-php-edit-file/#findComment-1232479
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
https://forums.phpfreaks.com/topic/239931-php-edit-file/#findComment-1232507
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
https://forums.phpfreaks.com/topic/239931-php-edit-file/#findComment-1232531
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.