dongwuzun4630 2017-11-23 07:01
浏览 21
已采纳

从php表单重命名文件

I have created a html form and a php which create and save a .txt file with the form input. It seams to be working fine, but how can I save the .txt file using the "Id" value given from the form. For instance if the user have an Id no. 1234 then the save .txt file should be 1234.txt (and not output.txt as in my code).

My form:

<!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>

and my 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
?>
  • 写回答

3条回答 默认 最新

  • douna4762 2017-11-23 07:04
    关注

    Simply put it as the name of the file like:

    <?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(); 
    

    And use w+ so it will create a new file if one doesn't already exist.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 这种微信登录授权 谁可以做啊
  • ¥15 请问我该如何添加自己的数据去运行蚁群算法代码
  • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”
  • ¥15 网络设备配置与管理这个该怎么弄
  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来