2006.10.20 Friday | 11:23

  PHP ファイルアップロード ふるいむかし

<?
header( "Content-Type: text/html; Charset=euc-jp" );
header( "Expires: Wed, 31 May 2000 14:59:58 GMT" );

$upload_dir = "file";
$redirect_url = "http://localhost/php/fileman/upload.php";
$return_url = "http://localhost/php/";
$err_message;


if ( $_POST['send'] == "アップロード" ) {

	$ret_file_name = $_FILES['MyUploadFile']['name'];

	if ( $_FILES['MyUploadFile']['error'] != 0 ) {
		$result_param =
			"ret=0&result="
			. urlencode("サイズが大きすぎるようです($ret_file_name)");
		header( "Location: $redirect_url?$result_param" );
		exit();
	}

	$target = $_FILES['MyUploadFile']['name'];
	$target = strtoupper($target);

	if ( is_uploaded_file( $_FILES['MyUploadFile']['tmp_name'] ) ) {
		move_uploaded_file(
			$_FILES['MyUploadFile']['tmp_name'],
			$upload_dir. "/" . $ret_file_name
		);
		$result_param =
				"ret=1&result="
				. urlencode("アップロードに成功しました($ret_file_name)");
	}
	else {
		$result_param =
			"ret=0&result="
			. urlencode("アップロードに失敗しました($ret_file_name)");
	}

	header( "Location: $redirect_url?$result_param" );
	exit();
}


?>
<HTML>
<HEAD>
<TITLE>ファイルマネージャ</TITLE>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; CHARSET=euc-jp">
<STYLE type="text/css">

* {
	font-size: 14px;
	font-family: Verdana;
}

a {
	font-weight: bold;
}

</STYLE>
<SCRIPT language="JavaScript">

	var dom,bIE;
	var objXMLHttp;

	if ( document.all ) {
		bIE = true;
		dom = new ActiveXObject("Msxml2.DOMDocument.3.0");
		objXMLHttp = new ActiveXObject("Msxml2.XMLHTTP.3.0");
	}
	else {
		bIE = false;
		dom = document.implementation.createDocument("", "", null);
		objXMLHttp = new XMLHttpRequest();
	}

function FileExist() {

	var result;

	var strPrefix = document.getElementsByName("prefix")[0].value;
	var strPath = document.getElementsByName("MyUploadFile")[0].value;

	if ( strPath == '' ) {
		alert( "ファイルを選択して下さい   " );
		return false;
	}

	strPrefix = encodeURIComponent(strPrefix);
	strPath = encodeURIComponent(strPath);

	objXMLHttp.open(
		"GET",
		"file_exist.php?path=" + strPath + "&prefix=" + strPrefix
		, false );
	objXMLHttp.send( "" );
	result = objXMLHttp.responseText;

	if ( result == 'OK' ) {
		if ( confirm( "このファイル名は既に存在します。置き換えますか?   " ) ) {
			return true;
		}
		else {
			return false;
		}
	}
	else {
		return true;
	}

}

</SCRIPT>
</HEAD>
<BODY bgcolor="Silver">

<FORM 
	enctype="multipart/form-data"
	method="POST"
	onSubmit='return FileExist()'
>
	<TABLE><tr>

	<TD>
	<INPUT type="hidden" name="MAX_FILE_SIZE" value="3000000">
	アップロードするファイル : 
	</TD>
	<TD>
	<INPUT name="MyUploadFile" type="file" style='width:400'>
	</TD>
	<TD>
	<INPUT type="submit" name="send" value="アップロード">
	</TD>
	<TD>
	<a href="<?= $return_url ?>">戻る</a>
	</TD>
	<TD>
	</TR>
	<TR>
	<TD colspan=4>
	ファイルの頭に<INPUT type=text name="prefix"> を付ける
	</TD>
 
	</tr></TABLE>

</FORM>
<?

	if ( $_GET['result'] != "" ) {
		if ( $_GET['ret'] == "0" ) {
			print "<SPAN style='color:red;font-weight:bold'>";
			print $_GET['result'];
			print "</SPAN><br><br>";
		}
		else {
			print "<SPAN style='color:blue;font-weight:bold'>";
			print $_GET['result'];
			print "</SPAN><br><br>";
		}
	}

	print "<TABLE border=0>";
	print "<TR>";
	print "<TD valign='top'>";

	$nRow = 0;

	$DirHandle = @opendir($upload_dir);
	if ( $DirHandle ) {
		$Target = readdir( $DirHandle );
		$file_cnt = 0;
		while( $Target !== false ) {
			$file_info[$file_cnt][0] = $Target;
			$file_info[$file_cnt][1] = filemtime( $upload_dir . "/" .  $Target );
			$sort_field[$file_cnt] = $file_info[$file_cnt][1];
			$Target = readdir( $DirHandle );
			$file_cnt++;
		}
		array_multisort( $sort_field, SORT_DESC, $file_info );

		print "<TABLE cellspacing=1 style='background-color:black' cellpadding=3>";
		for( $i = 0; $i < count( $file_info ); $i++ ) {
			$Target = $file_info[$i][0];
			if ( $Target != "." && $Target != ".." ) {
			   $nRow++;

			   print "<TR>";
			   print "<TD bgcolor='#FFFFFF'>$nRow</TD>";
			   print "<TD bgcolor='#FFFFFF'>";
			   print "<a style='font-size:11px' target='_blank' href='$upload_dir/"
			      . str_replace("+","%20",urlencode($Target)) . "'>";
			   print $Target;
			   print "</a></TD>";
			   print "</TR>";

			}
		}
		print "</TABLE>";
	}
	else {
		print "ディレクトリが不正です<br>";
	}


	print "</TR>";
	print "</TD>";
	print "</TABLE>";


?>
</BODY>
</HTML>