me102 Posted November 25, 2014 Share Posted November 25, 2014 Hello I have the following code from php websites $myfile = fopen("/config.php", "a+") or die("Unable to open file!"); while(!feof($myfile)) { echo fgets($myfile) . "<br>"; $txt = "Mickey Mouse\n"; fwrite($myfile, $txt); } inside config.php is <?php define('script_path', 'files'); ?> How can I replace files with another value? any help would be great thanks. Quote Link to comment Share on other sites More sharing options...
QuickOldCar Posted November 25, 2014 Share Posted November 25, 2014 <?php $my_file = "/config.php"; if (file_exists($my_file)) { /* //adds a new line $write = fopen($my_file, 'a+'); //rewind($write);//append to top $message = "Mickey Mouse\r\n"; fputs($write, $message); fclose($write); */ //note the w, this will overwrite the entire contents $write = fopen($my_file, 'w'); $message = "Mickey Mouse\r\n"; fputs($write, $message); fclose($write); } else { echo "$my_file doesn't exist"; } ?> Quote Link to comment Share on other sites More sharing options...
QuickOldCar Posted November 25, 2014 Share Posted November 25, 2014 These may be useful as well. is_writable() chmod() Quote Link to comment Share on other sites More sharing options...
me102 Posted November 25, 2014 Author Share Posted November 25, 2014 (edited) I think you may have miss understood me because of my poor english sorry. I am trying to use the script to replace define('script_path', 'files'); to define('script_path', 'NEWPATH'); Edited November 25, 2014 by me102 Quote Link to comment Share on other sites More sharing options...
Solution Ch0cu3r Posted November 25, 2014 Solution Share Posted November 25, 2014 Best way would be to edit the config.php yourself. But of want to PHP to do the edit for you then you'd need to loop over each line in the file. Find the line that defines that constant and then replace that line with the new value. Example <?php // the file to edit $config_file = 'config.php'; // the new value for the script_path constant $new_script_path_value = 'NEW/PATH/HERE'; // open the config file using file(). Strip the new lines from each line // each line will be separate item in the $lines array $lines = file($config_file, FILE_IGNORE_NEW_LINES); // loop over each line foreach($lines as $k => $line) { // find the line that defines the 'script_path' constant if(strpos($line, "define('script_path'") !== false) { // replace the constant with the new value $lines[$k] = "define('script_path', '$new_script_path_value');"; // use break to to exist the foreach loop. // No longer need to continue looping through the lines to find the constant as we have found it break; } } // implode the lines back into a string with newlines $file_contents = implode(PHP_EOL, $lines); // write the contents of the file back to the config file file_put_contents($config_file, $file_contents); Quote Link to comment Share on other sites More sharing options...
me102 Posted November 25, 2014 Author Share Posted November 25, 2014 Ch0cu3r your the freaking best thanks so much... :happy-04: :happy-04: Quote Link to comment 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.