edit file with PHP


6 Jan 2004
ok, I've just started php coding and I need some help. I need it to open a file and write to it, but I want it to write at the beginning of the file not the end of it. How can I do that?


I may actually be insane.
31 Mar 2002
I'm sure there's a way to actually write from the beginning, I used to have a file that did it, however this would seem the easiest method..
function write_beg($filename, $data) 
//Imports old data 
$handle = fopen($filename, "r"); 
$old_content = fread($handle, filesize ($filename)); 

//Sets up new data 
$final_content = $data.$old_content; 

//Writes new data 
$handle2 = fopen($filename, "w"); 
$finalwrite = fwrite($handle2, $final_content); 


OSNN Lurker
10 Feb 2004
Opening it with mode 'w' should give you the start of the file and 'a' should give you the end of the file. Alternatively 'r+' gives you read/write access from the begining of the file, but won't create the file if it doesn't exist like 'w' will.


OSNN Veteran Addict
18 Mar 2002
I used the following as a style.css editor, I have modified it down for you though. Basically the method it uses is to open the file you want to edit and then read the contents into a variable which is then placed in a <textarea>$variable</textarea> which makes it show up in the text area of the form. you can then modify that and submit at which point the data in the textarea on submission is then written to the original file and the contents of the original file is written to a backup file.

// setup stylesheet modification process
if ($_POST['submit'] == "update file") {
	$filename = "./style.css"; /* file to write to could be changed to a variable passed by the form */
	$copyname = "./style.copy.css"; /* using this makes a backup copy again this could be changed to a variable and have .bak appended to the filename */
	// open the style.css file to make a copy of the file
	$css_r = fopen($filename, "r");
	$style_copy = fread($css_r, filesize($filename));
	// as changes have been made lets make a backup
	$css_copy = fopen($copyname, "w");
	$fcopy_out = fwrite($css_copy, $style_copy);
	// copy is made now lets update the style.css file
	$css_w = fopen($filename, "w");
	$fstyle_out = fwrite($css_w, $_POST['contents']);
	// check if there were any problems writing to files.
	if ($fstyle_out != strlen($_POST['contents']) || $fcopy_out != strlen($style_copy)) {
		die ("Error writing to file\nPlease make sure the directory\nis CHMOD 777");
	} else {
		header ("Location: file_written.html"); /* this just redirects you to a new page which says the file has been written to */
} else {
	// open the style.css file and get the contents for the form
	$filename = "./style.css"; /* You could change this to modify a different file or you can get the $filename variable from another form or something */
	$fp = fopen($filename, "r");
	$contents = fread($fp, filesize($filename));
// setup html vars
$xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?".">";
$html = <<< EOF
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<title>Yoric's Place</title>
<link href="style.css" rel="stylesheet" type="text/css" />
<style type="text/css">
input {
	border: 1px solid #000000;
textarea {
	border: 1px solid #000000;
  <h2>File Modifier</h2>
    <form action="{$_SERVER['PHP_SELF']}" method="post">
      <p><br />
      <textarea name="contents" cols="40" rows="20" id="contents" wrap="none">{$contents}</textarea>
      <br />
      <input name="submit" type="submit" id="submit" value="update file" />

echo $xml;
echo $html;

Members online

No members online now.

Latest profile posts

Electronic Punk wrote on Sazar's profile.
Rest in peace my friend, been trying to find you and finally did in the worst way imaginable.
Terrahertz wrote on Electronic Punk's profile.
Yo fellas!
Electronic Punk wrote on Sazar's profile.
Where are you buddy?
Perris Calderon wrote on Electronic Punk's profile.
Hey EP! All good with me, applying for Microsoft MVP right now, should have done this a while ago.

Notifications don't work, I only found your response by coming back to hunt up some threads, if you want, give me your email address so we can keep in touch easier!
Perris Calderon wrote on Electronic Punk's profile.
EP, glad to see you come back and tidy up...did want to ask a one day favor, I want to enhance my resume , was hoping you could make me administrator for a day, if so, take me right off since I won't be here to do anything, and don't know the slightest about the board, but it would be nice putting "served administrator osnn", if can do, THANKS

Been running around Quora lately, luv it there!

Forum statistics

Latest member