今回は、ファイルを3つに分けたので、common.php と、view.php はダウンロードパッケージの中にあります。
dir.php
<?php
// *********************************************************
// 共通部分
// *********************************************************
require_once( 'common.php' );
// *********************************************************
// 変数
// *********************************************************
// データを作成するディレクトリ
$upload_dir = './upload';
// テーブルタグを含むファイル名リストの文字列
$upload_list = '';
// TABLE 一行の列数
$cols = 3;
////////////////////////////////////////////////////////////
// POST された場合の処理
////////////////////////////////////////////////////////////
if ( $_SERVER['REQUEST_METHOD'] == 'POST' ) {
if ( !file_exists( $upload_dir ) ) {
mkdir( $upload_dir );
}
$Message = "POSTされました \n";
// テスト用の簡単な ID 取得
$srcid = uniqid();
// ファイルへ入力されたデータを書き込み
file_put_contents( "$upload_dir/$srcid.txt", pack('C*',0xEF,0xBB,0xBF) . $_POST['text'] );
}
////////////////////////////////////////////////////////////
// 常に実行するファイル一覧の取得
////////////////////////////////////////////////////////////
$dir_handle = @opendir($upload_dir);
if ( $dir_handle ) {
// ループ処理
while( 1 ) {
$rows = read_row( $dir_handle, $cols );
// 配列が空の場合はデータが全く存在しないので、
// 何もしないですぐ終わる
if ( count( $rows ) == 0 ) {
break;
}
$upload_list .= "<tr>\n";
for( $i = 0; $i < $cols; $i++ ) {
$upload_list .= "<td>";
$upload_list .= "<a href=\"$upload_dir/{$rows[$i]}\"";
$upload_list .= " target=\"_blank\">{$rows[$i]}</a>";
$upload_list .= "</td>\n";
}
$upload_list .= "</tr>\n";
}
}
// *********************************************************
// HTML の TABLE の 一行分のデータを取得する関数
// 一行のデータを読んで、配列にデータをセットする
// ( 戻り値の配列数が 0 の場合、データは存在しない )
// $cols で、1行の列数を決定する
// *********************************************************
function read_row( $handle, $cols ) {
$ret = array();
while( 1 ) {
$target = readdir( $handle );
// データが完全な終了
if ( $target === false ) {
break;
}
// 対象外は読み飛ばし
if ( $target == '.' || $target == '..' ) {
continue;
}
$ret[] = $target;
// 指定回数配列がセットされたらループを終了する
if ( count( $ret ) == $cols ) {
break;
}
}
return $ret;
}
// *********************************************************
// 画面
// *********************************************************
require_once( 'view.php' );
?>
view.php
<!DOCTYPE html>
<html lang="ja">
<meta charset="utf-8">
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script type="text/javascript">
// *********************************************************
// フォームのチェック
// *********************************************************
function checkdata() {
var str = $("#text").get(0).value;
str = str.trim();
if ( str == '' ) {
alert("必須入力です");
$("#text").get(0).select();
$("#text").get(0).focus();
return false;
}
if ( !confirm( "更新しますか?" ) ) {
return false;
}
// サーバーへ送信
return true;
}
</script>
<style>
table {
margin-top: 10px;
border-collapse: collapse;
box-shadow: 0px 13px 17px -10px;
}
td {
border: solid 1px #444;
border-radius: 25px;
padding: 10px;
}
</style>
</head>
<body>
<form
name="frmMain"
method="post"
action="<?= $_SERVER['PHP_SELF'] ?>"
onsubmit='return checkdata()'>
<div>
<input type="submit" name="send" value="送信"> <?= $Message ?>
<input type="button" value="初期画面" onclick='location.href="<?= $_SERVER['PHP_SELF'] ?>"'>
<div>
<textarea id="text" name="text" cols="80" rows="15"><?= $_POST['text'] ?></textarea>
</form>
<table>
<?= $upload_list ?>
</table>
</body>
</html>
common.php
<?php
header( "Content-Type: text/html; charset=utf-8" );
header( "Expires: Thu, 19 Nov 1981 08:52:00 GMT" );
header( "Cache-control: no-cache" );
header( "Pragma: no-cache" );
foreach( $_GET as $key => $value ) {
$_POST[$key] = $_GET[$key];
}
?>
posted by
at 2018-01-31 16:17
|
スケルトン
|

|