我正在处理我的第一个PHP脚本,该脚本应该将文件上传到服务器,显示文件的内容,然后保存对文件的任何更改。
以下是代码:
<form action="phpfileedit.php" method="post" enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="file" id="file" />
<br/>
<button type="submit" name="submit" value="Submit">Submit button </button>
</form>
<?php
if(isset($_POST['submit'])) {
if ($_FILES["file"]["error"] > 0) {
echo "Error: " . $_FILES["file"]["error"] . " - Please select a file" . "<br />";
} else {
echo "Upload: " . $_FILES["file"]["name"] . "<br />";
echo "Type: " . $_FILES["file"]["type"] . "<br />";
echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
echo "Temporarily Stored in: " . $_FILES["file"]["tmp_name"] ." <br />";
echo "File Uploaded At: " . date("H:m");
echo $name;
}
move_uploaded_file($_FILES["file"]["tmp_name"], $_FILES["file"]["name"]);
echo "<br /><br />Your file was moved to: " . $_FILES["file"]["name"];
#file upload complete
echo "<br /><textarea name='textarea'>";
echo file_get_contents($_FILES["file"]["name"]);
echo "</textarea>";
}
if(isset($_POST['save'])) {
$file = $_FILES["file"]["name"];
$fh = fopen($file, 'w');
$txt = $_POST['textarea'];
fwrite($fh,$txt);
fclose($fh);
}
?>
<form action="phpfileedit.php" method="post" enctype="multipart/form-data">
<button type="submit" name="save" value="Submit">Save Changes</button>
</form>
处理文件写入的代码(不起作用):
if(isset($_POST['save'])) {
$file = $_FILES["file"]["name"];
$fh = fopen($file, 'w');
$txt = $_POST['textarea'];
fwrite($fh,$txt);
fclose($fh);
}
这应该在按下提交按钮时起作用,但它只是重置页面。
请您尽可能多地提供信息,因为我对编码很新。
提前感谢您的任何答案。