我想使用 fwrite 将数据保存到.txt文件中。该操作方法似乎有效,因为它可以在按提交时传输时显示 HTML 标签,但我不会运行 PHP。
<HTML lang="da">
<style>
</style>
<header>
<title>Tilføj</title>
<meta charset="ISO-8859-1">
</header>
<body>
<form method="post" action="eksamen_save_data.php" enctype='multipart/form-data'>
<fieldset>
<legend>Filmoplysninger</legend>
<div><label>Titel: <input type="text" name="titel" id="titel" required="required" size="60" maxlength="100"></label></div>
<div><label>Hovedskuespiller: <input type="text" name="hovedskuespiller" id="hovedskuespiller" required="required" size="30" maxlength="100"></label></div>
<div><label>Genre: <input type="text" name="genre" id="genre" required="required" size="60" maxlength="100"></label></div>
<div><label>Format: <input type="text" name="format" id="format" required="required" size="60" maxlength="100"></label></div>
<div><label>Billede: <input type="file" name="billede" id="billede" required="required"></label></div>
</fieldset>
<div><input type="submit" id="ok" value="OK"></div>
</form>
</body>
这会将其发送到如下所示的"eksamen_save_data.php":
<?php
$Titel = $_POST["titel"];
$Hovedskuespiller = $_POST["hovedskuespiller"];
$Genre = $_POST["genre"];
$Format = $_POST["format"];
//$Billede = $_FILES["billede"]["navn"];
//if($_FILES){
// move_uploaded_file($_FILES["billed"]["navn"], $_FILES["billed"]["navn"]);
//}
$user_data = "$Titel, $Hovedskuespiller, $Genre, $Format, $Billede 'r'n";
$fh = fopen("data.txt", "a") or die("Fejl i åbning af fil!");
fwrite($fh, $user_data) or die ("Fejl i skrivning til fil!");
fclose($fh);
?>
如果我在"eksamen_save_data.php"中编写一些 HTML,我可以显示它,但它不会运行 PHP。我正在使用XAMPP。问题是它不会像我告诉 PHP 那样保存到"data.txt"文件中。
另一个问题;有没有办法,我可以让PHP在与我的字段集相同的文件中运行?
最后编辑:
大多数时候,小错误被证明是最大的问题。对我来说,我个人忘记在浏览器中使用:localhost/eksamen_tilf øj.php。
所以我在XAMPP中犯了一个错误。
使用 file_put_contents
file_put_contents("data.txt", $user_data, FILE_APPEND);
它完成所有工作,如文件打开,写入和关闭。优点是如果文件不存在,那么它将创建。
查找完整的工作代码
<?php
$Titel = $_POST["titel"];
$Hovedskuespiller = $_POST["hovedskuespiller"];
$Genre = $_POST["genre"];
$Format = $_POST["format"];
$Billede = $_FILES["billede"]["name"];
// Example of accessing data for a newly uploaded file
$fileName = $_FILES["billede"]["name"];
$fileTmpLoc = $_FILES["billede"]["tmp_name"];
// Path and file name
$pathAndName = "upload/".$fileName;
// Run the move_uploaded_file() function here
$moveResult = move_uploaded_file($fileTmpLoc, $pathAndName);
// Evaluate the value returned from the function if needed
if ($moveResult == true) {
echo "File has been moved from " . $fileTmpLoc . " to" . $pathAndName;
} else {
echo "ERROR: File not moved correctly";
}
$user_data = "$Titel, $Hovedskuespiller, $Genre, $Format, $Billede 'r'n";
file_put_contents("data.txt", $user_data, FILE_APPEND);
?>
>您必须删除第三个参数$test
(因为它指定了要写入的内容的长度)。但是$test
没有在你的PHP文件中定义,所以它不会写任何东西。
所以改变这个
fwrite($fh, $user_data, $test) or die ("Fejl i skrivning til fil!");
进入这个
fwrite($fh, $user_data) or die ("Fejl i skrivning til fil!");
看看这个:)
至于你的第二个问题:当然你可以合并你的表单并提交脚本:
<?php
if(count($_POST) > 0) { //
/** Form submit function, file write **/
} else {
?>
<html>
<form action="#" method="POST">
<!-- Enter HTMLform here -->
</form>
</html>
<?php } ?>
这个伪代码并不漂亮,但在解释东西方面可以做。表单操作中的#
意味着提交时将调用相同的脚本。if(count($_POST) > 0)
检查是否已提交数据。如果是这样,则将写入文件。否则,将显示表单。
祝你好运。