以下 php 程序中的文件处理概念错误是什么?
what is the error in file handling concept in below php program ?
每当我尝试使用创建按钮创建一个新的 txt 文件时,虽然文件已创建但数据未写入文件。我只是一个空白文件。错误是什么?
<?php
error_reporting(0);
if(isset($_POST['sub']))
{
$fname=$_POST["txtfile"];
$cont=$_POST['txtarea'];
if($_POST['sub']=="create")
{
fopen($fname,"w");
fwrite($fname, $cont);
}
else if($_POST['sub']=="read")
{
echo file_get_contents($fname);
}
else if($_POST['sub']=="delete")
{
unlink($fname);
}
else if($_POST['sub']=="append")
{
$cont=$_POST['txtarea'];
$fp=fopen($fname,"a");
fwrite($fp,$cont);
}
}
?>
这里是上面程序的html代码。我应该将用于读取文件的 php 代码插入 html 中的文本区域代码吗?
<!DOCTYPE html>
<html>
<h3>file handling concept</h3>
<style type="text/css">
#form{
height: 200px;
width: 400px;
margin: 10px;
}
label {
float: left;
margin-right: 10px;
width: 70px;
padding-top: 5px;
font-size: 15px;
}
#form input, #form textarea{
padding: 5px;
width: 306px;
font-family: Helvetica, sans-serif;
font-size: 15px;
margin: 0px 0px 0px 0px;
border: 2px solid #ccc;
}
#button input{
float: right;
margin: 5px;
width: 60px;
padding: 5px;
border: 1px solid #ccc;
}
</style>
<div id="form">
<form method="POST" action="filehandling.php">
<label for="name">Filename:</label>
<input type="text" name="txtfile" placeholder="file-name.txt" />
<label for="name">content:</label>
<textarea name="txtarea" placeholder="write some content">
</textarea>
<div id="button">
<input type='submit' name='sub' value='create'>
<input type='submit' name='sub' value='delete'>
<input type='submit' name='sub' value='append'>
<input type='submit' name='sub' value='read'>
</div>
</form>
</div>
</html>
我认为您在创建文件时也应该使用句柄,如下所示。
if($_POST['sub']=="create")
{
$fh = fopen($fname,"w");
fwrite($fh, $cont);
fclose($fh);
}
此外,在 else if($_POST['sub']=="append")
中,无需再次定义 $cont
,因为它已经定义了相同的值。
所以
else if($_POST['sub']=="append")
{
$cont=$_POST['txtarea'];
$fp=fopen($fname,"a");
fwrite($fp,$cont);
}
变成
else if($_POST['sub']=="append")
{
$fp=fopen($fname,"a");
fwrite($fp,$cont);
fclose($fp);
}
您的文件创建和写入方法应该如下:
if($_POST['sub']=="create")
{
$fp = fopen($fname,"wb");
fwrite($fp,$cont);
fclose($fp);
}
每当我尝试使用创建按钮创建一个新的 txt 文件时,虽然文件已创建但数据未写入文件。我只是一个空白文件。错误是什么?
<?php
error_reporting(0);
if(isset($_POST['sub']))
{
$fname=$_POST["txtfile"];
$cont=$_POST['txtarea'];
if($_POST['sub']=="create")
{
fopen($fname,"w");
fwrite($fname, $cont);
}
else if($_POST['sub']=="read")
{
echo file_get_contents($fname);
}
else if($_POST['sub']=="delete")
{
unlink($fname);
}
else if($_POST['sub']=="append")
{
$cont=$_POST['txtarea'];
$fp=fopen($fname,"a");
fwrite($fp,$cont);
}
}
?>
这里是上面程序的html代码。我应该将用于读取文件的 php 代码插入 html 中的文本区域代码吗?
<!DOCTYPE html>
<html>
<h3>file handling concept</h3>
<style type="text/css">
#form{
height: 200px;
width: 400px;
margin: 10px;
}
label {
float: left;
margin-right: 10px;
width: 70px;
padding-top: 5px;
font-size: 15px;
}
#form input, #form textarea{
padding: 5px;
width: 306px;
font-family: Helvetica, sans-serif;
font-size: 15px;
margin: 0px 0px 0px 0px;
border: 2px solid #ccc;
}
#button input{
float: right;
margin: 5px;
width: 60px;
padding: 5px;
border: 1px solid #ccc;
}
</style>
<div id="form">
<form method="POST" action="filehandling.php">
<label for="name">Filename:</label>
<input type="text" name="txtfile" placeholder="file-name.txt" />
<label for="name">content:</label>
<textarea name="txtarea" placeholder="write some content">
</textarea>
<div id="button">
<input type='submit' name='sub' value='create'>
<input type='submit' name='sub' value='delete'>
<input type='submit' name='sub' value='append'>
<input type='submit' name='sub' value='read'>
</div>
</form>
</div>
</html>
我认为您在创建文件时也应该使用句柄,如下所示。
if($_POST['sub']=="create")
{
$fh = fopen($fname,"w");
fwrite($fh, $cont);
fclose($fh);
}
此外,在 else if($_POST['sub']=="append")
中,无需再次定义 $cont
,因为它已经定义了相同的值。
所以
else if($_POST['sub']=="append")
{
$cont=$_POST['txtarea'];
$fp=fopen($fname,"a");
fwrite($fp,$cont);
}
变成
else if($_POST['sub']=="append")
{
$fp=fopen($fname,"a");
fwrite($fp,$cont);
fclose($fp);
}
您的文件创建和写入方法应该如下:
if($_POST['sub']=="create")
{
$fp = fopen($fname,"wb");
fwrite($fp,$cont);
fclose($fp);
}