gamesmad Posted July 23, 2006 Share Posted July 23, 2006 Hey,Does anyone have any working code that will allow me to open a file, find part of it, replace that part with something else, and then save the file??Thanks,Will Quote Link to comment https://forums.phpfreaks.com/topic/15382-modifying-a-file-with-php/ Share on other sites More sharing options...
Joe Haley Posted July 23, 2006 Share Posted July 23, 2006 http://php.net/manual/en/function.fopen.phphttp://php.net/manual/en/function.fread.phphttp://php.net/manual/en/function.fwrite.phpIf you have any questions after reading, then please, do ask them ^^ Quote Link to comment https://forums.phpfreaks.com/topic/15382-modifying-a-file-with-php/#findComment-62352 Share on other sites More sharing options...
gamesmad Posted July 23, 2006 Author Share Posted July 23, 2006 So using fopen will bind the contents of the file to the variable?? I dont understand what fread does, something to do with reading the filesize?? I understand fwrite. Hope you can answer.Thanks,Will Quote Link to comment https://forums.phpfreaks.com/topic/15382-modifying-a-file-with-php/#findComment-62363 Share on other sites More sharing options...
kenrbnsn Posted July 23, 2006 Share Posted July 23, 2006 The fopen() function [b]OPENS[/b] the file. The fread() function [b]READS[/b] the file one record at a time. The frwrite() function [b]WRITES[/b] records to a file. You should also look at the file() function http://www.php.net/file and the file_get_contents() function http://www.php/net/file_get_contents. The file() function reads the entire file into an array. The file_get_contents() function reads the entire file into a string.Read the manual pages that have been referenced and look at the examples.When you have some code written than you need help with, post said code and ask your questions.Ken Quote Link to comment https://forums.phpfreaks.com/topic/15382-modifying-a-file-with-php/#findComment-62367 Share on other sites More sharing options...
Joe Haley Posted July 23, 2006 Share Posted July 23, 2006 fopen takes a filename, and a mode. (read the documentation for explination of modes)fopen returns a resource.fread takes a resource (returnd from fopen), and an integer.fread returns a string of the contents of the file opend with fopen.fwrite takes a resource (returnd from fopen), a string, and an integer.fwrite writes data to the file.fclose should be called after you have opend, read, and written to the file.fclose takes a resource (returnd from fopen)So, a program to read a file, and write to it, would contain the basic formulation of:open the file;read the contents;write contents to the file;close the file;Use the functions explained above to achive this. Quote Link to comment https://forums.phpfreaks.com/topic/15382-modifying-a-file-with-php/#findComment-62368 Share on other sites More sharing options...
gamesmad Posted July 24, 2006 Author Share Posted July 24, 2006 OK thanks guys, I will produce some of mine code, and Ill come back if it doesnt work.Will Quote Link to comment https://forums.phpfreaks.com/topic/15382-modifying-a-file-with-php/#findComment-62754 Share on other sites More sharing options...
gamesmad Posted July 24, 2006 Author Share Posted July 24, 2006 OK, I just whipped up this -[code]<?phprequire "config.php";$settingscontents = file_get_contents("$boarddir/$account/Settings.php", "r+");echo "$settingscontents";?>[/code]Where do I go from here, fread() seems to only look through a file by bytes?? I want to find some text in the file and replace it with something else.Will Quote Link to comment https://forums.phpfreaks.com/topic/15382-modifying-a-file-with-php/#findComment-62762 Share on other sites More sharing options...
Joe Haley Posted July 24, 2006 Share Posted July 24, 2006 fread($handle, filesize($filename))where $filename is the name of the file you opend with fopen.To write a slightly modified version of the file, open it with the proper mode, manipulate the string returnd by fread however you want, and then write it back into the file (or even write it to a diffrent file) Quote Link to comment https://forums.phpfreaks.com/topic/15382-modifying-a-file-with-php/#findComment-62766 Share on other sites More sharing options...
gamesmad Posted July 24, 2006 Author Share Posted July 24, 2006 How would I use fread to find part of it, and replace it with something else??Will Quote Link to comment https://forums.phpfreaks.com/topic/15382-modifying-a-file-with-php/#findComment-62815 Share on other sites More sharing options...
Joe Haley Posted July 24, 2006 Share Posted July 24, 2006 You wouldnt.You would use fread to get the entier file.Then, you would use other functions to find and replace part of it.then, you would use fwrite to write the modified file. Quote Link to comment https://forums.phpfreaks.com/topic/15382-modifying-a-file-with-php/#findComment-62822 Share on other sites More sharing options...
kenrbnsn Posted July 24, 2006 Share Posted July 24, 2006 Use the file() function instead. This will read the entire file into an array, then you just have to search the array for what you want to replace and write the file back. Something like this:[code]<?php$input = file("$boarddir/$account/Settings.php");$fp = fopen("$boarddir/$account/Settings.php","w");$output = array();foreach($input as $line_no =>$line) { $output[$line_no] = str_replace($old_str,$new_str,$line); if($output[$line_no] != $line) echo "$old_str was replace with $new_str on line $line_no<br>\n"; fwrite($fp,$output[$line_no]);}fclose($fp);?>[/code]There are other ways of doing this, see the manual pages for str_replace() (http://www.php.net/str_replace) and follow the referecences to other functions.Ken Quote Link to comment https://forums.phpfreaks.com/topic/15382-modifying-a-file-with-php/#findComment-62838 Share on other sites More sharing options...
gamesmad Posted July 24, 2006 Author Share Posted July 24, 2006 I cannot thank you enough :) I have finally got my script finished :PThanks,Will Quote Link to comment https://forums.phpfreaks.com/topic/15382-modifying-a-file-with-php/#findComment-62887 Share on other sites More sharing options...
gamesmad Posted July 25, 2006 Author Share Posted July 25, 2006 Is it possible to replace a whole line, regardless of content, with str_replace?? If not, what could I use instead??Will Quote Link to comment https://forums.phpfreaks.com/topic/15382-modifying-a-file-with-php/#findComment-63275 Share on other sites More sharing options...
gamesmad Posted July 25, 2006 Author Share Posted July 25, 2006 Anyone??Will Quote Link to comment https://forums.phpfreaks.com/topic/15382-modifying-a-file-with-php/#findComment-63419 Share on other sites More sharing options...
kenrbnsn Posted July 25, 2006 Share Posted July 25, 2006 If you want to replace the whole line, find the line you want using the foreach loop. When found, just write the new line to the file instead of the old line.Ken Quote Link to comment https://forums.phpfreaks.com/topic/15382-modifying-a-file-with-php/#findComment-63430 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.