dakke Posted December 29, 2009 Share Posted December 29, 2009 Hi all,Probably very easy for you guys, for a newbie... not really.I try to replace a string in a file through ftp. Not sure how it should be done.I assume you have to open the file, than str_replace and than save the file again? The following is probably completely wrong, and as such to show how far I am from the solution. <form method="post"><input name="db_name" type="text" /><input type="submit" class="submit" value="Check database >>" /></form><?php$db_login = "../includes/db_login.php";$fh1 = fopen($db_login, 'w');$str = str_replace("test", $db_name, $str);?> Where I try to replace 'test', with $db_name in the file "db_login.php", using the $db_name coming from the form. Link to comment Share on other sites More sharing options...
Synook Posted December 29, 2009 Share Posted December 29, 2009 You should be able to just use file_get_contents and file_put_contents, e.g. <form action="" method="post"><input name="db_name" type="text" /><input type="submit" class="submit" value="Check database >>" /></form><?phpif (isset($_POST['db_name'])) {$db_login = "../includes/db_login.php";$fh1 = file_get_contents($db_login);$str = str_replace("test", $_POST['db_name'], $str);file_put_contents($db_login, $str);}?> Note: this doesn't use FTP, and you shouldn't need FTP either.Note 2: you should perform some sort of sanitizing on the form field, or else someone could insert arbitrary PHP into your file. Link to comment Share on other sites More sharing options...
dakke Posted December 29, 2009 Author Share Posted December 29, 2009 You should be able to just use file_get_contents and file_put_contents, e.g. <form action="" method="post">Thanks for the quick reply.I always get a blank file using the above. When I submit, the entire file's content is deleted and not replaced. It does not replace, it deletes. What am I doing wrong? Link to comment Share on other sites More sharing options...
Synook Posted December 29, 2009 Share Posted December 29, 2009 Eh, logical error, the return value of file_get_contents() should be assigned to $str. <form action="" method="post"><input name="db_name" type="text" /><input type="submit" class="submit" value="Check database >>" /></form><?phpif (isset($_POST['db_name'])) {$db_login = "../includes/db_login.php";$str = file_get_contents($db_login);$str = str_replace("test", $_POST['db_name'], $str);file_put_contents($db_login, $str);}?> Link to comment Share on other sites More sharing options...
dakke Posted December 29, 2009 Author Share Posted December 29, 2009 I'll try to play around with it, since it still (with new code) has the same behavior, namely clearing the file. Link to comment Share on other sites More sharing options...
Synook Posted December 29, 2009 Share Posted December 29, 2009 Hmm, maybe file_get_contents() gets the interpreted content. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.