dongqu4443 2018-11-18 14:11
浏览 88

PHP-将输入保存到具有日期名称的单独文件

I did a code in PHP where input is saved to the file 'news'. Now I would like to change it that it will save every input to separate file in special folder(news) which name starts with date.

I wrote something like this but it isn't working (the file isn't being created)

<?php
include "0begin.php";

$title=$_POST["title"];
isset($title) or $title=$_GET["title"];
$msg=$_POST["msg"];

?>
<h1>News</h1>

<form method=post>
Title<br><input type=text input name="title" value=<?=$title?> ><br>
Message<br>
<textarea input name="msg" cols=40 rows=5> </textarea><br>
<input type="submit">
<br><br>
</form>

<?php
$msg = $_POST['msg'];
$dateposted = date("YmdHis");
$fp = fopen("$dateposted.txt", "w"); 
fwrite($fp,$title, $msg).'&nbsp;'; 
fclose($fp); 
?> 
<?php
include "0end.php";
  • 写回答

1条回答 默认 最新

  • dtk31564 2018-11-18 15:32
    关注

    I think your Problem lies in the fopen and fwrite you are using. fopen ("$dateposted.txt","w") will create you a file with the name $dateposted.txt. I think you rather want fopen($dateposted."txt","w").

    Since the file isn't created, I would check the folder permissions of the destination folder and modify them if the user that runs the script, for example an apache on linux isn't allowed to write there. Or set the complete Destinationpath to be sure the file is stored in the correct destionation.

    Another thing regarding the fwrite. From what I know, fwrite accepts 2 Parameters. File and String. The third possible parameter is the maximum length of added bytes. In your case I would call fwrite once for the title and once for the message. Furthermore the string concatenation on the fwrite looks wrong to me, since I assume the concated String should be in the file you would need to write for example fwrite($fp, $title.'&nbsp;');

    评论

报告相同问题?

悬赏问题

  • ¥15 matlab中使用gurobi时报错
  • ¥15 WPF 大屏看板表格背景图片设置
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂