Я делаю блог без использования базы данных. Таким образом, у меня есть форма ввода, которая при отправке должна обрабатываться файлом php, который создает файлы для записей. Мой вопрос: как хранить несколько файлов? Благодарю!
<?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
}
Вы можете использовать это:
$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);
Дополнительная информация: больше обработки файлов в PHP