簡要掲示板を作るという自分への課題をしております。
HMTLフォームから名前、コメントを入力しテキストファイルに
番号<>名前<>コメント<>日時を保存し、HTMLフォームの下に表示するというものを
作っております。
できないこと
消去機能を追加しようとしているのですが、うまくいきません。
このソースを実行すると下記のようにエラーがでて、調べると文字列ではなく配列が入力されている
とかでファイルに書き込むことができません。
{Warning: file_put_contents() expects parameter 1 to be string, resource given in /home/co-981.99sv-coco.com/public_html/Kadai2/Kadai2_15.php on line 50}
php
1 2//htmlファイル 3<?php 4header('Content-Type: text/html; charset=UTF-8'); 5 6require_once('Kadai2_15.php'); 7?> 8<html> 9 <head> 10 <meta charset="utf-8"/> 11 <title>sample</title> 12 </head> 13 <body> 14 <form method="post" action="<?php echo($_SERVER['PHP_SELF']) ?>"> 15 <p>名前:</p> 16 <input type="text" name="name" placeholder="山田太郎" required="required"> 17 <p>コメント:</p> 18 <textarea type="text" name="com" maxlength="20" required></textarea> 19 <!-- <input type="hidden" name="token" value="<?=h(sha1(session_id())) ?>"> --> 20 <input type="submit" name="mode" value="add"> 21 </form> 22 <form action="<?php echo($_SERVER['PHP_SELF']) ?>" method="POST"> 23 <p>削除対象番号:</p> 24 <input type="text" name="deleteNo"> 25 <input type="submit" name="delete" value="削除"> 26 <input type="hidden" name="mode" value="delete"> 27 </form> 28 <p> 29 <p> 30 掲示板: 31 </p> 32 <?php 33 34 foreach ((array)$text_rows as $text_row) {//配列でcount<>name<>comment<>data表示 35 if($text_row === ""){ 36 continue; 37 } 38 $ret = explode("<>", $text_row); 39 echo h($ret[0])."番 "; 40 echo h($ret[1])." さん "; 41 echo h($ret[2])." "; 42 echo h($ret[3]); 43 echo "<br>"; 44 45 } 46 ?> 47 48 </p> 49 </body> 50</html>
php
1//php 2 3<?php 4header('Content-Type: text/html; charset=UTF-8'); 5?> 6<html> 7 <head> 8 <meta charset="utf-8"/> 9 <title>sample</title> 10 </head> 11 <body> 12 13 <?php 14 15 function h($str) { 16 return htmlspecialchars($str, ENT_QUOTES, 'UTF-8'); 17 } 18 19 //session_start(); // 1 20 21 $name = (string)filter_input(INPUT_POST, 'name'); //名前フォーム値 文字列にキャスト 22 $comment = (string)filter_input(INPUT_POST, 'com');//コメントフォーム値 フィルタリングする 23 //$token = (string)filter_input(INPUT_POST, 'token'); // トークンセッション 24 25 26 switch($_POST["mode"]){ 27 case "add"://投稿追加処理 28 $fp = fopen('Kadai2_15.txt', 'a+'); 29 if ($_SERVER['REQUEST_METHOD'] === 'POST'/* && sha1(session_id()) === $token */) { // postが送られて来てるか?CSRF対策 30 flock($fp, LOCK_SH); 31 $text_rows = file("Kadai2_15.txt"); 32 $count = count($text_rows);//行数をカウント 33 $add_text = ($count + 1)."<>".$name."<>".trim($comment)."<>".date('H時i分');//追加したい文章 34 array_unshift($text_rows, $add_text); 35 flock($fp, LOCK_EX); 36 fwrite($fp, $add_text."\n"); 37 flock($fp, LOCK_UN); 38 fclose($fp); 39 } 40 break; 41 case "delete"://投稿削除処理 42 if (isset($_POST["delete"])) { 43 $fp = fopen('Kadai2_15.txt', 'a+'); 44 $delete = $_POST["deleteNo"]; 45 flock($fp, LOCK_SH); 46 $delarray = file("Kadai2_15.txt"); 47 for ($j = 0; $j < count($delarray) ; $j++){ 48 $delData = explode("<>", $delarray[$j]); 49 if ($delData[0] == $delete) { 50 array_splice($delarray, $j, 1); 51 flock($fp, LOCK_EX); 52 file_put_contents($fp, implode("\n", $delarray));//問題の箇所 53 break; 54 55 } 56 } 57 58 break; 59 } 60 } 61 62 63 64 ?> 65 </body> 66 67</html>
まだまだ未熟者で汚いソースと思いますが、よろしくお願いします。
var_dump($delarray)の出力結果 ↓
{
array(4) { [0]=> string(54) "1<>アンパンマン<>アンパンラブ<>15時46分 " [1]=> string(54) "2<>ジャムおじさん<>ジャムラブ<>15時47分 " [2]=> string(54) "3<>バタコさん<>バター塗りたい<>15時47分 " [3]=> string(45) "4<>チーズ<>キャンキャン<>15時47分 " }
}
回答2件
あなたの回答
tips
プレビュー