PHP: ファイルのアップロード

jqueryupload.js の実装です。
[ bassjobsen/jqueryupload ](https://github.com/bassjobsen/jqueryupload/)
サーバーサイドは、PHP を使いました。
data_work というフォルダーにアップロードされます。

フォルダー構造

```text
$ tree
.
├── data_work -> /var/tmp/data_work/
├── file_upload.html
├── file_upload.js
├── file_upload.php
└── index.html -> file_upload.html
```

```html:file_upload.html
<!DOCTYPE html>
<html lang="ja">
<head>
<meta http-equiv="CONTENT-TYPE" content="text/html; charset=utf-8" />
<meta content="Uchida Masatomo" name="author">
<script src="/js/jquery-3.6.0.min.js"></script>
<script src="/js/jqueryupload.js"></script>
<script src="file_upload.js"></script>
<title>file_upload (Apr/09/2021)</title>
</head>
<body>
<h2>file_upload</h2>
<hr />
送るファイルを選んで下さい。<br />
<hr />
<input id="upload" type="file" name="upload_file">
<hr />
<div id="result"></div>
<hr />
<div id="outarea_aa">outarea_aa</div>
<div id="outarea_bb">outarea_bb</div>
<div id="outarea_cc">outarea_cc</div>
<div id="outarea_dd">outarea_dd</div>
<div id="outarea_ee">outarea_ee</div>
<div id="outarea_ff">outarea_ff</div>
<div id="outarea_gg">outarea_gg</div>
<div id="outarea_hh">outarea_hh</div>
<hr />
Apr/09/2021 AM 08:25<p />
</body>
</html>
```

```js:file_upload.js
// -----------------------------------------------------------------------
//	file_upload.js
//
//					Apr/09/2021
//
// -----------------------------------------------------------------------
jQuery (function ()
{
	jQuery("#outarea_aa").text ("*** start *** file_upload.js ***")

	upload_file_proc ()

	jQuery("#outarea_hh").text ("*** end *** file_upload.js ***")
});

// -----------------------------------------------------------------------
function upload_file_proc ()
{
	jQuery('input[type=file]').change(function() {
		jQuery(this).upload('file_upload.php', function(res) {

		var str_tmp = "*** File uploaded ***"
		jQuery("#outarea_dd").text (str_tmp)
//		jQuery("#outarea_ee").text (JSON.stringify(res))
		jQuery("#result").html (res.filename + " is uploaded.<br />")
//		jQuery("#outarea_ff").text (JSON.stringify(res.message))

			var str_out = ""

			for (var it in res.message)
				{
				var line = res.message[it]
				str_out += line + '<br />'
				}

			jQuery("#outarea_gg").html (str_out)
		}, 'json')
})

}

// -----------------------------------------------------------------------
```

```php:file_upload.php
<?php
// ------------------------------------------------------------------
//	file_upload.php
//
//						Apr/09/2021
//
// ------------------------------------------------------------------
$message = array ();

$now = $_POST['now'];
$message[] =  "file_upload.php *** start *** Apr/09/2021";

$folder = 'data_work';

$message[] =  "folder = " . $folder;
$message[] = "now = " . $now;

$tmp_name_xx = $_FILES["upload_file"]["tmp_name"];
$filename = $_FILES['upload_file']['name'];
$message[] = "aaa tmp_name_xx = " . $tmp_name_xx;
$message[] = "aaa filename = " . $filename;

$message = upload_proc ($folder,$filename,$tmp_name_xx,$message);

$message[] = "*** upload_file.php *** end *** ";
$result = array ();
$result['folder'] = $folder;
$result['filename'] = $filename;
$result['message'] = $message;
$str_json = json_encode ($result);

print	$str_json;
// ------------------------------------------------------------------
function upload_proc ($folder,$filename,$tmp_name_xx,$message)
{
	$message[] = "upload_proc folder = " . $folder;
	$message[] = "upload_proc filename = " . $filename;

	$llx = strlen ($filename);

	if ($llx == 0)
		{
		$message[] = '<h2><span id="red">*** error p ***</span></h2>';
		$message[] = "<blockquote>";
		$message[] = "Please select a file.";
		$message[] = "</blockquote>";
		$message[] = "*** Upload failed ***";
		}
	else
		{
		$message[] = "*** upload_proc *** ppp  ***";
		$message = upload_exec_proc ($folder,$filename,$tmp_name_xx,$message);
		}


	return	$message;
}

// ------------------------------------------------------------------
function upload_exec_proc ($folder,$filename,$tmp_name_xx,$message)
{
	$message[] = "upload_proc_exec *** start *** " . $filename;

	$path_target = $folder . '/' . $filename;

	$message[] = "*** upload_exec_proc *** " . $folder . " ***";
	$message[] = "*** upload_exec_proc *** " . $path_target . " ***";

	if (move_uploaded_file
		($tmp_name_xx,$path_target) == FALSE)
		{
	        $message[] = 'nothing is uploaded at ' . $_POST['now'];
		$message[] = $filename . " cannot be uploaded.";
		}
	else
		{
		$message[] = 'Uploaded at' . $_POST['now'];
		$message[] = 'Folder : ' . $folder;
		$message[] = $filename  . " is uploaded";

		$message = info_display_proc ($path_target,$message);
		}

	return	$message;
}

// ------------------------------------------------------------------
// [8-4]:
function info_display_proc ($path_target,$message)
{
	$message[] = "path_target = " . $path_target;
	$message[] = "REMOTE_ADDR = " . $_SERVER["REMOTE_ADDR"];
	$message[] = "SERVER_NAME = " . $_SERVER["SERVER_NAME"];
	$message[] = "SERVER_ADDR = " . $_SERVER["SERVER_ADDR"];
	$message[] = "HTTP_USER_AGENT = " . $_SERVER["HTTP_USER_AGENT"];

	$url =  "http://" . $_SERVER["SERVER_ADDR"];
	$url .= "/data_base/examples/file_upload/" .  $path_target;

//	$message[] = "<blockquote>";
//	$message[] = '<span id="green">' . $url . '</span>';
//	$message[] = "</blockquote>";

	return	$message;
}

// ------------------------------------------------------------------
?>
```