php实现编辑和保存文件的方法
程序员文章站
2023-11-28 08:42:22
本文实例讲述了php实现编辑和保存文件的方法。分享给大家供大家参考。具体如下:
save_file.php:
本文实例讲述了php实现编辑和保存文件的方法。分享给大家供大家参考。具体如下:
save_file.php:
<?php session_start(); $handle = fopen($_post['original_file_name'], "w"); $text = $_post['file_contents']; if(fwrite($handle, $text) == false){ $_session['error'] = '<span class="redtxt">there was an error</span>'; }else{ $_session['error'] = '<span class="redtxt">file edited successfully</span>'; } fclose($handle); header("location: ".$_post['page']); ?>
read_file.php:
<!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="content-type" content="text/html; charset=iso-8859-1" /> <title>untitled document</title> </head> <form action="savecontents.php" method="post"> <textarea name="file_contents" style="width:700px;height:600px;"> <?php $filename = "location/of/orignal/file/my_file.php"; $handle = fopen($filename, "r"); while (!feof($handle)){ $text = fgets($handle); echo $text; } ?> </textarea> <input type="hidden" value=" <? echo $filename; ?> " name="original_file_name" /> </form> <body> </body> </html>
希望本文所述对大家的php程序设计有所帮助。