Linjon Posted June 20, 2011 Share Posted June 20, 2011 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!"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/239931-php-edit-file/ Share on other sites More sharing options...
Brandon_R Posted June 20, 2011 Share Posted June 20, 2011 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); Quote Link to comment https://forums.phpfreaks.com/topic/239931-php-edit-file/#findComment-1232479 Share on other sites More sharing options...
Linjon Posted June 20, 2011 Author Share Posted June 20, 2011 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 :/ Quote Link to comment https://forums.phpfreaks.com/topic/239931-php-edit-file/#findComment-1232507 Share on other sites More sharing options...
requinix Posted June 20, 2011 Share Posted June 20, 2011 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 ); Quote Link to comment https://forums.phpfreaks.com/topic/239931-php-edit-file/#findComment-1232531 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.