Jump to content

Modifying a file with PHP


gamesmad

Recommended Posts

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.