从php表单重命名文件
我创建了一个html表单和一个php,用表单输入创建并保存一个.txt文件。它看起来工作正常,但是如何使用表单中给定的“Id”值保存.txt文件呢。例如,如果用户的Id号为1234,那么save.txt文件应该是1234.txt(而不是像我的代码中那样的output.txt) 我的表格:从php表单重命名文件,php,forms,Php,Forms,我创建了一个html表单和一个php,用表单输入创建并保存一个.txt文件。它看起来工作正常,但是如何使用表单中给定的“Id”值保存.txt文件呢。例如,如果用户的Id号为1234,那么save.txt文件应该是1234.txt(而不是像我的代码中那样的output.txt) 我的表格: <!DOCTYPE HTML> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta content="t
<!DOCTYPE HTML>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
<title>Untitled 1</title>
</head>
<body>
<form action="form.php" method="post">
<ol>
<li><label for="email">Email</label>
<input type="text" name="email" id="email"></li>
<li><label for="id">Id</label>
<input type="text" name="id" id="id"></li>
</ol>
</form>
</body>
</html>
无标题1
电子邮件
身份证
还有我的php
<?php
ob_start(); // start trapping output
$id = @$_POST['id'];
$email = @$_POST['email'];
?>
<html>
<body>
<p>
Id: <?php echo $id; ?><br>
Email: <?php echo $email; ?>
</p>
</body>
</html>
<?php
$output = ob_get_contents(); // get contents of trapped output
//write to file, e.g.
$newfile="output.txt";
$file = fopen ($newfile, "w");
fwrite($file, $output);
fclose ($file);
ob_end_clean(); // discard trapped output and stop trapping
?>
Id:
电邮:
只需将其作为文件名,如:
<?php
$output = ob_get_contents();
//write to file, e.g.
$newfile="{$id}.txt";
$file = fopen ($newfile, "w+");
fwrite($file, $output);
fclose ($file);
ob_end_clean();
您正在静态地指定文件名。只需像这样动态地给出文件名
$filename = $id.'.txt';
更改php文件中的行:
$newfile=“output.txt”
将其更改为:
$newfile=“{$id}.txt”
这会有用的 $newfile=$id.“.txt”
应该可以工作