"Diary" インターネットさんへの恩返し

いつもソースコードコピペばかりなので,みなさまへ少しばかりの恩返しを

サーバ上のhtmlファイル等をブラウザ経由で編集するプログラム(php)



スポンサーリンク

いちいちFTPでアップするの面倒くさいのでテキストエディタの作成 - PHPでファイルの読み書き・掲示板 - ポンクソフトソースコードをありがたく頂き、バックアップ機能やデザインをちょっとキレイにしました。変更した元ファイルをbackupフォルダに<ファイル名>_yyyymmdd_hhmmddで自動保存します。これに認証機能も付けたいところですが、それはまた時間があるときに。


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html lang="ja">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>テキストエディタ</title>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>

<script>

$(function() {
    $(".list").dblclick(function () {
		$('#open_btn').trigger("click");
    });
});


</script>
<style>

*{
font-family:メイリオ;
}

td{
vertical-align:top;
}

table{
width:100%;
}

textarea{
width:100%;
height:480px;
}

.btn{
width:100px;
padding:5px;
margin-top:10px;
background: -webkit-gradient(linear, left top, left bottom, color-stop(0.00, #4175ff), color-stop(1.00, #213fb1));
background: -webkit-linear-gradient(#4175ff, #213fb1);
background: -moz-linear-gradient(#4175ff, #213fb1);
background: -o-linear-gradient(#4175ff, #213fb1);
background: -ms-linear-gradient(#4175ff, #213fb1);
background: linear-gradient(#4175ff, #213fb1);
color:#fff;
}

#filelist{
width:100%;
}

</style>
</head>
<body>
<h1>Direct Editer</h1>
<form method="post" action="このファイル名.php">
<table>
<tr>
<td width="20%">
<span>ファイル選択</span><br>
<select id="filelist" name="file" size="20">
<?php
// ファイル一覧を表示
$dir = opendir("./");
while($file = readdir($dir)) {
  if(is_file("./$file")) {
	if(strstr($file, '.html')){
	    print "<option class='list'>$file</option>\n";
	}
  }
}
closedir($dir);
?>
</select>
<br />

<input onclick="location.reload()" value="一覧を再読込" type="submit" class="btn">
<input id="open_btn" type="submit" name="open" value="開く" class="btn">

</td>
<td width="75%">
<span>編集して、保存ボタンを押して下さい。(Ctrl+fキーで文字検索ができるよ)</span>
<textarea name="contents">
<?php
// ファイル内容を表示
$file = $_POST['file'];
if ($_POST['open'] && $file) {
  $text = file_get_contents($file);
  $text = htmlspecialchars($text);
  print $text;
}
?>
</textarea>
<br />
<input type="submit" name="save" value="保存" class="btn">

<?php
// ファイルを保存
$editfile = $_POST['editfile'];
if ($_POST['save'] && $editfile) {

  if (!copy($editfile, "backup/".$editfile."_".date('Ymd_His'))) {
    echo "failed to copy $file...\n";
  }

  $fp = @fopen($editfile, 'w');
  if (!$fp) print "このファイルには書き込みできません。<br>\n";
  else {


    $contents = $_POST['contents'];
    fwrite($fp, $contents);
    fclose($fp);
    print "書き込み完了しました。<br>\n";
  }
}
?>

</td>
</tr>
</table>
<input type="hidden" name="editfile" value="<?php print $file ?>">
</form>
</body>
</html>

こんなイメージ。
f:id:azumami:20151113004802p:plain