Jump to content


Photo

Modifying a file with PHP


  • Please log in to reply
14 replies to this topic

#1 gamesmad

gamesmad
  • Members
  • PipPip
  • Member
  • 14 posts

Posted 23 July 2006 - 10:56 AM

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

#2 Joe Haley

Joe Haley
  • Members
  • PipPipPip
  • Advanced Member
  • 103 posts
  • LocationCanada, eh?

Posted 23 July 2006 - 11:06 AM

http://php.net/manua...ction.fopen.php
http://php.net/manua...ction.fread.php
http://php.net/manua...tion.fwrite.php

If you have any questions after reading, then please, do ask them ^^
Give a man a fish; you have fed him for today.  Teach a man to fish; and you have fed him for a lifetime
Don't teach men to program. Teach them to fish.

Please, try the RTFM solution before asking for help:
http://php.net/manual/en/index.php

#3 gamesmad

gamesmad
  • Members
  • PipPip
  • Member
  • 14 posts

Posted 23 July 2006 - 01:04 PM

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

#4 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 23 July 2006 - 02:03 PM

The fopen() function OPENS the file. The fread() function READS the file one record at a time. The frwrite() function WRITES 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

#5 Joe Haley

Joe Haley
  • Members
  • PipPipPip
  • Advanced Member
  • 103 posts
  • LocationCanada, eh?

Posted 23 July 2006 - 02:07 PM

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.
Give a man a fish; you have fed him for today.  Teach a man to fish; and you have fed him for a lifetime
Don't teach men to program. Teach them to fish.

Please, try the RTFM solution before asking for help:
http://php.net/manual/en/index.php

#6 gamesmad

gamesmad
  • Members
  • PipPip
  • Member
  • 14 posts

Posted 24 July 2006 - 11:43 AM

OK thanks guys, I will produce some of mine code, and Ill come back if it doesnt work.

Will

#7 gamesmad

gamesmad
  • Members
  • PipPip
  • Member
  • 14 posts

Posted 24 July 2006 - 11:57 AM

OK, I just whipped up this -

<?php

require "config.php";

$settingscontents = file_get_contents("$boarddir/$account/Settings.php", "r+");

echo "$settingscontents";

?>

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

#8 Joe Haley

Joe Haley
  • Members
  • PipPipPip
  • Advanced Member
  • 103 posts
  • LocationCanada, eh?

Posted 24 July 2006 - 12:04 PM

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)
Give a man a fish; you have fed him for today.  Teach a man to fish; and you have fed him for a lifetime
Don't teach men to program. Teach them to fish.

Please, try the RTFM solution before asking for help:
http://php.net/manual/en/index.php

#9 gamesmad

gamesmad
  • Members
  • PipPip
  • Member
  • 14 posts

Posted 24 July 2006 - 01:25 PM

How would I use fread to find part of it, and replace it with something else??

Will

#10 Joe Haley

Joe Haley
  • Members
  • PipPipPip
  • Advanced Member
  • 103 posts
  • LocationCanada, eh?

Posted 24 July 2006 - 01:37 PM

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.
Give a man a fish; you have fed him for today.  Teach a man to fish; and you have fed him for a lifetime
Don't teach men to program. Teach them to fish.

Please, try the RTFM solution before asking for help:
http://php.net/manual/en/index.php

#11 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 24 July 2006 - 01:53 PM

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:
<?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);
?>
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


#12 gamesmad

gamesmad
  • Members
  • PipPip
  • Member
  • 14 posts

Posted 24 July 2006 - 02:51 PM

I cannot thank you enough :)  I have finally got my script finished :P

Thanks,

Will

#13 gamesmad

gamesmad
  • Members
  • PipPip
  • Member
  • 14 posts

Posted 25 July 2006 - 06:24 AM

Is it possible to replace a whole line, regardless of content, with str_replace??  If not, what could I use instead??

Will

#14 gamesmad

gamesmad
  • Members
  • PipPip
  • Member
  • 14 posts

Posted 25 July 2006 - 02:52 PM

Anyone??

Will

#15 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 25 July 2006 - 03:23 PM

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




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users