■ 画面部分と処理部分を分割
■ ファイルアップロード処理の実装
■ アップロード後にリダイレクト
■ 処理を2会話に分けて、入力コントロール
<?
header( "Content-Type: text/html; Charset=shift_jis" );
header( "pragma: no-cache" );
header( "Expires: Wed, 31 May 2000 14:59:58 GMT" );
header( "Cache-control: no-cache" );
// 以下IE用
header( "P3P: CP=\"CAO PSA OUR\"" );
$MAX_SIZE = "500000";
$REDIRECT = "http://{$_SERVER["SERVER_NAME"]}{$_SERVER["PHP_SELF"]}";
$DISABLE_ATT = "disabled style='background-color:silver'";
foreach( $_GET as $Key => $Value ) {
$_POST[$Key] = $_GET[$Key];
}
foreach( $_POST as $Key => $Value ) {
$_POST[$Key] = str_replace("\\\\", "\\", $Value );
$_POST[$Key] = str_replace("\\'", "'", $_POST[$Key] );
$_POST[$Key] = str_replace("\\\"", "\"", $_POST[$Key] );
}
foreach( $_COOKIE as $Key => $Value ) {
$_COOKIE[$Key] = str_replace("\\\\", "\\", $Value );
$_COOKIE[$Key] = str_replace("\\'", "'", $_COOKIE[$Key] );
$_COOKIE[$Key] = str_replace("\\\"", "\"", $_COOKIE[$Key] );
}
// クッキーによる入力値の保存
if ( $_POST['outfile'] != '' ) {
setcookie("outfile", $_POST['outfile'], time()+60*60*24*30 );
}
else {
if ( $_COOKIE['outfile'] != '' ) {
$_POST['outfile'] = $_COOKIE['outfile'];
}
else {
$_POST['outfile'] = "text.dat";
}
}
// 送信ボタン(submit) によって送られて来た場合
if ( $_SERVER['REQUEST_METHOD'] == 'POST' ) {
if ( $_POST['send'] == '送信' ) {
$Message = "POSTされました<br>\n";
// ファイルへ入力されたデータを書き込み
file_put_contents( $_POST['outfile'], $_POST['text'] );
$disabled_head = "disabled style='background-color:silver'";
}
if ( $_POST['send'] == 'アップロード' ) {
if ( $_FILES['MyUploadFile']['error'] == UPLOAD_ERR_OK ) {
$upload_file =
realpath("./") .
DIRECTORY_SEPARATOR .
$_FILES['MyUploadFile']['name'];
if ( move_uploaded_file(
$_FILES['MyUploadFile']['tmp_name'], $upload_file ) ) {
header("Location: {$REDIRECT}");
exit();
}
else {
$Message = "アップロードに失敗しました<br>\n";
$disabled_head = $DISABLE_ATT;
}
}
else {
$Message = "アップロードに失敗しました<br>\n";
$disabled_head = $DISABLE_ATT;
}
}
}
// HTTP 通信部分
if ( $_SERVER['REQUEST_METHOD'] == 'GET' ) {
if ( $_POST['target'] != '' ) {
// ファイルを読み込み
print file_get_contents( $_POST['target'] );
exit();
}
else {
if ( isset( $_POST['target'] ) ) {
exit();
}
else {
$disabled_body = $DISABLE_ATT;
}
}
}
require_once("view.php");
?>
view.php ( 画面部分 )
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=shift_jis" />
<style>
* {
font-size:30px;
}
</style>
<script
src="http://www.google.com/jsapi"
type="text/javascript"
charset="utf-8"
></script>
<script type="text/javascript">
// Google で HOSTINGされているライブラリを使う
google.load("prototype", "1.6");
// *********************************************************
// フォームのチェック
// *********************************************************
function checkdata() {
var str = $("text").value
str = str.strip();
if ( str == '' ) {
alert("必須入力です");
$("text").select();
$("text").focus();
return false;
}
if ( !confirm( "更新しますか?" ) ) {
return false;
}
// サーバーへ送信
return true;
}
// *********************************************************
// HTTP 通信
// *********************************************************
function getText() {
file_name = $("outfile").value;
// ■ サーバー側コードは get_text.php
// ■ GET
// ■ 同期処理
// ■ target=filename
// オブジェクト作成 = イベント作成です
new Ajax.Request("<?= $_SERVER['PHP_SELF'] ?>",
{
method: "get",
asynchronous: false,
parameters: { "target" : file_name },
onSuccess: function(request) {
// 成功した事が保証される
},
onComplete: function(request) {
$("text").value = request.responseText;
},
onFailure: function(request) {
alert('読み込みに失敗しました ');
}
}
);
}
</script>
</head>
<body>
<input type=button value="初期画面" onClick='location.href="<?= $_SERVER['PHP_SELF'] ?>"'>
<input type=button value="HTTP通信" onClick='getText();'>
<form
name="frmmain"
method="post"
action="<?= $_SERVER['PHP_SELF'] ?>"
onsubmit='return checkdata()'
>
出力ファイル名
<input
type=text
id="outfile"
name="outfile"
size=40
value="<?= $_POST['outfile'] ?>"
<?= $disabled_head ?>
>
<br>
<textarea
id=text
name=text
cols=80 rows=5
<?= $disabled_head ?>
></textarea>
<br />
<input
type=submit
name=send
value="送信"
<?= $disabled_head ?>
> <?= $Message ?>
</form>
<form
name="frmmain"
method="post"
action="<?= $_SERVER['PHP_SELF'] ?>"
enctype="multipart/form-data"
>
<br>
<INPUT type="hidden" name="MAX_FILE_SIZE" value="<?= $MAX_SIZE ?>">
アップロードするファイル :
<INPUT
name="MyUploadFile"
type="file"
style='width:400'
<?= $disabled_body ?>
>
<INPUT
type="submit"
name=send
value="アップロード"
<?= $disabled_body ?>
>
</form>
</body>
</html>
■ 関連する記事
Form(サーバー送信)汎用スケルトン
Form(サーバー送信)汎用スケルトン(2)
Form(サーバー送信)汎用スケルトン(3)
■ 関連する PHP ドキュメント
ファイルアップロードのエラーメッセージの説明
posted by
at 2009-06-26 12:06
|
スケルトン
|

|