本文实例讲述了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程序设计有所帮助。
如对本文有疑问, 点击进行留言回复!!
[ACTF2020 新生赛]Include 1【文件包含】【读取代码】
算法题解 - 牛客编程巅峰赛S1第6场 - 黄金&钻石&王者组
推荐一个程序员必备网站之一:Dev Hints ,非常实用!
网友评论