Jump to content

Archived

This topic is now archived and is closed to further replies.

gamesmad

Modifying a file with PHP

Recommended Posts

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

Share this post


Link to post
Share on other sites
http://php.net/manual/en/function.fopen.php
http://php.net/manual/en/function.fread.php
http://php.net/manual/en/function.fwrite.php

If you have any questions after reading, then please, do ask them ^^

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
OK thanks guys, I will produce some of mine code, and Ill come back if it doesnt work.

Will

Share this post


Link to post
Share on other sites
OK, I just whipped up this -

[code]<?php

require "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

Share this post


Link to post
Share on other sites
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)

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
Is it possible to replace a whole line, regardless of content, with str_replace??  If not, what could I use instead??

Will

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.