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).' ';
fclose($fp);
?>
<?php
include "0end.php";
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.' ');