duanfei1987 2014-03-03 19:43
浏览 238
已采纳

fwrite函数不向文件写入任何内容


I am trying to write data into a txt file, but It won't work. there is no error. it just won't work.
here is the code

if (isset($_REQUEST['submit']))
    {
        $hour = $_REQUEST['hour'];
        $family = $_REQUEST['family'];
        $how = $_REQUEST['how'];
        $will = $_REQUEST['will'];
        $fun = $_REQUEST['fun'];
        $kind = $_REQUEST['kind'];
        $device = $_REQUEST['device'];
        $study = $_REQUEST['study'];
        $agree = $_REQUEST['agree'];
        $text = "hour :" . $hour . "<br />" . "family" . $family . "<br />" . "تطابفق با درس" . $how . "<br />" . "سرگرمی های غیر مجازی : " . $will . "<br />" . "نوع سرگرمی :" . $fun . "<br />" . "kind :  " . $kind . "<br />" . "device " . $device . "<br />" . "اثیر بر درس" . $study . "<br />" . "کنترل " . $agree . "<br />" ;
        fopen('/1.txt', "r");
        fwrite('/1.txt',"hour :" . $hour . "<br />" . "family" . $family . "<br />" . "تطابفق با درس" . $how . "<br />" . "سرگرمی های غیر مجازی : " . $will . "<br />" . "نوع سرگرمی :" . $fun . "<br />" . "kind :  " . $kind . "<br />" . "device " . $device . "<br />" . "اثیر بر درس" . $study . "<br />" . "کنترل " . $agree . "<br />");
        fclose('/1.txt');

    }

do note that there is an "else" that includes all the aforementioned input fields that I used in the code , and that I am executing on localhost. thanks

  • 写回答

4条回答 默认 最新

  • dqv2743 2014-03-03 19:45
    关注

    You cannot write to it since you are opening it as read-only:

    $handle = fopen('/1.txt', "r");
    

    Instead:

    $handle = fopen('/1.txt', "w"); // to write only, if you need to read and write use 'w+'
    

    You also need to store fopen in a $handle so you can write to it later.

    Documentation: http://www.php.net/manual/en/function.fopen.php

    Now the first parameter for fwrite should be $handle:

    fwrite($handle, "hour :" . $hour . "<br />" . "family" . $family . "<br />" . "تطابفق با درس" . $how . "<br />" . "سرگرمی های غیر مجازی : " . $will . "<br />" . "نوع سرگرمی :" . $fun . "<br />" . "kind :  " . $kind . "<br />" . "device " . $device . "<br />" . "اثیر بر درس" . $study . "<br />" . "کنترل " . $agree . "<br />");
    

    Documentation: http://www.php.net/manual/en/function.fwrite.php

    At the end you should also close with $handle:

    fclose($handle);
    

    Documentation: http://www.php.net/manual/en/function.fclose.php

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

报告相同问题?

悬赏问题

  • ¥15 远程桌面文档内容复制粘贴,格式会变化
  • ¥15 关于#java#的问题:找一份能快速看完mooc视频的代码
  • ¥15 这种微信登录授权 谁可以做啊
  • ¥15 请问我该如何添加自己的数据去运行蚁群算法代码
  • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”
  • ¥15 网络设备配置与管理这个该怎么弄
  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题