时间:2021-07-01 10:21:17 帮助过:24人阅读
本文实例讲述了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>
<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网站建设的流程
PHP中感知哈希算法
php程序中字符串长度判断
以上就是php针对文件的读取、编辑和保存的操作的详细内容,更多请关注Gxl网其它相关文章!