edit file with PHP

Jan 6, 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.
Mar 31, 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
Feb 10, 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
Mar 18, 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

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 comming back to hunt up some threads, if you want, give me your email address so we can keep in touch easier, mine is perriscalderon at gmail
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 https://tinyurl.com/ycpxl
Electronic Punk wrote on Perris Calderon's profile.
All good still mate?
Hello, is there anybody in there? Just nod if you can hear me ...

Forum statistics

Latest member