テキストエディタ -PHP入門サンプル置き場

\"PHP入門サンプル置き場\"

PHP入門向けのサンプルソ\ースを公開しています。

  

PHPも自由に扱えて安い!おすすめのレンタルサーバーです。

テキストエディタ

txteditor.php

<html> <head> <meta http-equiv=\"Content-Type\" content=\"text/html; charset=Shift_JIS\"> <title>テキストエディタ</title> </head> <body> <form method=\"POST\" action=\"txteditor.php\"> 編集したいファイルを選びましょう <table> <tr> <!---左のセル---> <td> <select name=\"file\" size=\"20\"> <?php //同階層ファイルの一覧表\示 $dir=opendir(\"./\"); while($file=readdir($dir)){ if(is_file(\"./$file\")){ echo \"<option>$file</option>\\n\"; } } closedir($dir); ?> </select> </td> <!---右のセル---> <td> <textarea name=\"nakami\" cols=\"60\" rows=\"20\"> <?php //指定したファイルの内容を表\示 $file=$_POST[\'file\']; if($_POST[\'open\'] && $file){ $txt=file_get_contents($file); $txt=htmlspecialchars($txt); echo($txt); } ?> </textarea> </td> </tr> <tr> <td align=\"right\"> <input type=\"submit\" name=\"open\" value=\"ファイルを開く\"> </td> <td align=\"right\"> <input type=\"submit\" name=\"save\" value=\"更新\"> </td> </tr> </table> <input type=\"hidden\" name=\"editfile\" value=\"<?php echo($file); ?>\"> </form> <?php //ファイルの内容更新 $editfile=$_POST[\'editfile\']; if($_POST[\'save\'] && $editfile){ $fp=@fopen($editfile, \'w\'); if(!$fp) echo \"ファイルを更新できません<br>\\n\"; else{ $contents=htmlspecialchars($_POST[\'nakami\']); fputs($fp,$contents); fclose($fp); echo \"データを更新しました<br>\\n\"; } } ?> </body> </html>

実行ページ

補足情報

opendir( )、readdir( )、closedir( )はセットで確認しておきましょう。
ここではwhileで繰り返しならが、echoで<option>タグを出力しています。フォームから$_POST[ ]を使ってデータを取得します。ここでは、txteditor.phpと同階層に置いているmemo1.txt、memo2.txt、memo3.txtに読み書きさせています。

Copyright (C) PHP入門サンプル置き場 All Rights Reserved.