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