2017-11-23 07:01
浏览 20


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:

<html xmlns="">

<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
<title>Untitled 1</title>


<form action="form.php" method="post">

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



and my php

    ob_start(); // start trapping output
    $id = @$_POST['id'];
    $email = @$_POST['email'];

Id: <?php echo $id; ?><br>
Email: <?php echo $email; ?>
    $output = ob_get_contents(); // get contents of trapped output
    //write to file, e.g.
    $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:

    $output = ob_get_contents();
    //write to file, e.g.
    $file = fopen ($newfile, "w+"); 
    fwrite($file, $output); 
    fclose ($file);  

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

    解决 无用
    打赏 举报
  • 查看更多回答(2条)

相关推荐 更多相似问题