<!DOCTYPE htmlL PUBLIC \"-//W3C//DTD htmlL 4.01 Transitional//EN\" \"http://www.w3.org/TR/htmll4/loose.dtd\"> <htmll lang=\"ja\"> <head> <meta http-equiv=\"Content-Type\" content=\"text/htmll; charset=Shift_JIS\"> <title>テキストエディタ −PHP入門サンプル置き場</title> <meta name=\"keywords\" content=\"PHP,入門,サンプル,ソ\ース,テキストエディタ\"> <meta name=\"description\" content=\"PHP入門向けのサンプルソ\ースを公開しています。\"> <meta http-equiv=\"Content-Style-Type\" content=\"text/css\"> <link href=\"../style.css\" rel=\"stylesheet\" type=\"text/css\"> </head> <!-- body部分 --> <body> <!-- #page ページの整形:中央寄せとか --> <div id=\"page\"> <!-- #header 画面上部のヘッド部分 --> <div id=\"head\"><a href=\"../index.html\"><img src=\"../img/logo.gif\" width=\"400\" height=\"60\" alt=\"PHP入門サンプル置き場\" border=\"0\"></a></div> <!-- #menu メニュー部分 --> <div id=\"menu\"> <ul> <li><a href=\"../index.html\">HOME</a></li> <li><a href=\"../link/index.html\">リンク</a></li> </ul> </div> <h1>PHP入門向けのサンプルソ\ースを公開しています。</h1> <!-- #main 本文スペース --> <div id=\"submenu\">    <ul> <li><a href=\"../counter/textcounter/index.html\">テキストカウンター</a></li> <li><a href=\"../counter/imgcounter/index.html\">画像カウンター</a></li> <li><a href=\"../counter/ytcounter/index.html\">昨日今日カウンター</a></li> <li><a href=\"../counter/vcounter/index.html\">個別訪問カウンター</a></li> <li><a href=\"../counter/rencounter/index.html\">連続IP処理カウンター</a></li> <li><a href=\"index.html\">テキストエディタ</a></li> <li><a href=\"../calendar/index.html\">カレンダー</a></li> <li><a href=\"../questionnaire/index.html\">アンケート</a></li> <li><a href=\"../mailform/index.html\">メールフォーム</a></li> <li><a href=\"../password/index.html\">パスワード処理</a></li> <li><a href=\"../bbs/index.html\">一行掲示板</a></li> <li><a href=\"../urlende/index.html\">URLエンコード・デコード</a></li> <li><a href=\"../moziretu/index.html\">文字列の置換</a></li> </ul> <!-- ロリポップ --> <div id=\"lolipop\"> <a href=\"http://px.a8.net/svt/ejp?a8mat=15QWN6+BLCVUA+348+67RK1\" target=\"_blank\"> <img border=\"0\" width=\"120\" height=\"60\" alt=\"\" src=\"http://www20.a8.net/svt/bgt?aid=070119618701&wid=001&eno=01&mid=s00000000404001044000&mc=1\"></a> <img border=\"0\" width=\"1\" height=\"1\" src=\"http://www10.a8.net/0.gif?a8mat=15QWN6+BLCVUA+348+67RK1\" alt=\"\"> </div> <p class=\"lolipoptxt\">PHPも自由に扱えて安い!おすすめのレンタルサーバーです。</p> </div> <!-- #main 本文スペース --> <div id=\"main\"> <h2>テキストエディタ</h2> <p>txteditor.php</p> <p><code> &lt;html&gt; &lt;head&gt; &lt;meta http-equiv=\"Content-Type\" content=\"text/html; charset=Shift_JIS\"&gt; &lt;title&gt;テキストエディタ&lt;/title&gt; &lt;/head&gt; &lt;body&gt; &lt;form method=\"POST\" action=\"txteditor.php\"&gt; 編集したいファイルを選びましょう &lt;table&gt; &lt;tr&gt; &lt;!---左のセル---&gt; &lt;td&gt; &lt;select name=\"file\" size=\"20\"&gt; &lt;?php //同階層ファイルの一覧表\示 $dir=opendir(\"./\"); while($file=readdir($dir)){ if(is_file(\"./$file\")){ echo \"&lt;option&gt;$file&lt;/option&gt;\\n\"; } } closedir($dir); ?&gt; &lt;/select&gt; &lt;/td&gt; &lt;!---右のセル---&gt; &lt;td&gt; &lt;textarea name=\"nakami\" cols=\"60\" rows=\"20\"&gt; &lt;?php //指定したファイルの内容を表\示 $file=$_POST[\'file\']; if($_POST[\'open\'] && $file){ $txt=file_get_contents($file); $txt=htmlspecialchars($txt); echo($txt); } ?&gt; &lt;/textarea&gt; &lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td align=\"right\"&gt; &lt;input type=\"submit\" name=\"open\" value=\"ファイルを開く\"&gt; &lt;/td&gt; &lt;td align=\"right\"&gt; &lt;input type=\"submit\" name=\"save\" value=\"更新\"&gt; &lt;/td&gt; &lt;/tr&gt; &lt;/table&gt; &lt;input type=\"hidden\" name=\"editfile\" value=\"&lt;?php echo($file); ?&gt;\"&gt; &lt;/form&gt; &lt;?php //ファイルの内容更新 $editfile=$_POST[\'editfile\']; if($_POST[\'save\'] && $editfile){ $fp=@fopen($editfile, \'w\'); if(!$fp) echo \"ファイルを更新できません&lt;br&gt;\\n\"; else{ $contents=htmlspecialchars($_POST[\'nakami\']); fputs($fp,$contents); fclose($fp); echo \"データを更新しました&lt;br&gt;\\n\"; } } ?&gt; &lt;/body&gt; &lt;/html&gt; </code> </p> <p><a href=\"txteditor.php\" target=\"_blank\">実行ページ</a></p> <h2>補足情報</h2> <p>opendir( )、readdir( )、closedir( )はセットで確認しておきましょう。<br> ここではwhileで繰り返しならが、echoで&lt;option&gt;タグを出力しています。フォームから$_POST[ ]を使ってデータを取得します。ここでは、txteditor.phpと同階層に置いているmemo1.txt、memo2.txt、memo3.txtに読み書きさせています。</p> <!-- Googleアドセンス --> <p> <script type=\"text/javascript\"><!-- google_ad_client = \"pub-4773670194351031\"; google_ad_width = 468; google_ad_height = 60; google_ad_format = \"468x60_as\"; google_ad_type = \"text\"; //2007-04-08: PHP入門サンプル置き場 google_ad_channel = \"6831583382\"; //--> </script> <script type=\"text/javascript\" src=\"http://pagead2.googlesyndication.com/pagead/show_ads.js\"> </script> </p> </div> <!-- #foot 画面一番下 --> <div id=\"foot\"> <p>Copyright (C) PHP入門サンプル置き場 All Rights Reserved.</p> </div> </div> </body> </htmll>