将html表单数据存储到php文件中

时间:2014-03-20 10:14:06

标签: php html

我在不使用数据库的情况下制作博客。所以我有一个添加条目表单,提交时应由php文件处理,该文件为条目创建文件。我的问题是如何存储多个文件? 谢谢!

2 个答案:

答案 0 :(得分:0)

你可以使用这个:

创建文件

$my_file = 'file.txt';
$handle = fopen($my_file, 'w') or die('Cannot open file:  '.$my_file); //implicitly creates file

写入文件

$my_file = 'file.txt';
$handle = fopen($my_file, 'w') or die('Cannot open file:  '.$my_file);
$data = 'This is the data';
fwrite($handle, $data);

更多信息:more file handling in PHP

答案 1 :(得分:0)

<?php
session_start();
$username = $_SESSION['username']; //this allows the user to return to blog and edit      before submitting
$filename = $username . "/entry.txt";

$handle = fopen($filename, "a+");
$entrydata = fread($handle, filesize($filename)); //opens file for reading, and reads the file for any text that may already exist
fclose($handle); //close the handle

if (!empty($_POST['posted'])){ //maybe add this as a hidden field in your form to verify     if the form was actually posted

  $entry = $_POST['entry']; //saves text that has been entered in the entry form
  $handle = fopen($filename, "w+");

  if (flock($handle, LOCK_EX)){  //an exclusive lock to file
   if (fwrite($handle, $entry) == FALSE){
    echo "Cannot write to file";
   }
 flock ($handle, LOCK_UN); //release the lock
 }
}
else{
//some code
}