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'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); 
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.
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

Also Hi EP and people. I found this place again while looking through a oooollllllldddd backup. I have filled over 10TB and was looking at my collection of antiques. Any bids on the 500Mhz Win 95 fix?
Any of the SP crew still out there?
Xie wrote on Electronic Punk's profile.
Impressed you have kept this alive this long EP! So many sites have come and gone. :(

Just did some crude math and I apparently joined almost 18yrs ago, how is that possible???
hello peeps... is been some time since i last came here.
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.

Forum statistics

Latest member