Оглавление Главная страница |
Редактор файла (Class): <? // Редактор файла (Class) // Версия 1.2 // (C) Андрей Латышев 2001 // www.programmer.spb.ru class EditFile { var $FileName; var $FieldName="FileText"; var $SubmittedName="FileTexSubmittedt"; var $Loaded=false; var $FileText; var $cols=30; var $rows=5; var $FormSubmitted=0; function EditFile($FileName) { $this->FileName=$FileName; $this->LoadFromFile(); $this->LoadFromForm(); } function FormLoaded() { return $this->Loaded; } function ShowForm($AdditionalFields="") { $Result="<form action=\"".$GLOBALS["SCRIPT_NAME"]."\" method=\"post\">"; $Result.="<textarea name=\"".$this->FieldName."\""; $Result.=" cols=".$this->cols." rows=".$this->rows.">"; $Result.=$this->FileText."</textarea>"; $Result.="<input type=\"hidden\" name=\"".$this->SubmittedName."\" value=\"1\">"; $Result.=$AdditionalFields; $Result.="<br><input type=\"submit\" value=\"Сохранить\">"; return $Result; } function LoadFromForm() { if ($GLOBALS[$this->SubmittedName]==1) { $this->Loaded=true; return $GLOBALS[$this->FieldName]; } } function LoadFromFile() { $fp = fopen ($this->FileName, "r"); $this->FileText = fread ($fp, filesize ($this->FileName)); fclose ($fp); } function SaveToFile($Text) { $fp = fopen ($this->FileName, "w"); fwrite($fp, $Text); fclose ($fp); } function FormToFile() { $text=$this->LoadFromForm(); $text=str_replace("\\\"","\"",$text); $this->SaveToFile($text); } } ?> Пример использования: <!-- Ваш HTML тут --> <? require("edit_file.php"); $FilePath="myfile.txt"; $ef=new EditFile($FilePath); $ef->cols=60; $ef->rows=20; if (!$ef->Loaded) { echo "<center>".$ef->ShowForm()."</center>"; }else { $ef->FormToFile(); echo "<p align=center>Изменения сохранены</p>"; } ?> <!-- Ваш HTML тут --> |
| © Андрей Латышев 1999-2002 | E-mail: |