Хранить данные HTML-формы в php-файле

0

Я делаю блог без использования базы данных. Таким образом, у меня есть форма ввода, которая при отправке должна обрабатываться файлом php, который создает файлы для записей. Мой вопрос: как хранить несколько файлов? Благодарю!

  • 0
    Какие файлы вы хотите экспортировать?
  • 0
    @fiskerXO Он хочет хранить в текстовых файлах
Теги:

2 ответа

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
}
  • 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);

Дополнительная информация: больше обработки файлов в PHP

Ещё вопросы

Сообщество Overcoder
Наверх
Меню