Ajaxの単一ファイルのアップロード

htmlコードは次の

<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
	
		<title>单文件上传</title>
	</head>
	
	<body>
		<label>请选择需要上传的文件:</label>
		<input type="file" name="simpleFile" id="simpleFile" />
		<button id="btn">上传</button>
		
		<script>
			document.getElementById('btn').onclick=function(){
				if(window.XMLHttpRequest){
					var xhr = new XMLHttpRequest();
				} else {
					var xhr = new ActiveXObject('Microsoft XMLHTTP');
				}
				
				xhr.open('POST','http://localhost/demo/input1/file.php','true');
				xhr.setRequestHeader('X-Requested-With','XMLHttpRequest');
				var formData = new FormData();
				formData.append('file',document.getElementById('simpleFile').files[0]);
				xhr.send(formData);
				
				xhr.onreadystatechange=function(){
					if(xhr.readyState===4){
						if(xhr.status===200){
							console.log('文件发送成功'+xhr.responseText);
						} else {
							alert('ERROR:'+xhr.status);
						}
					}
				}
			}
		</script>
	</body>
</html>

次のようにPHPのコードは次のとおりです。

<?php
	header('Content-Type:text/html;charset=UTF-8');
	header("Access-Control-Allow-Origin: *");
	header('Access-Control-Allow-Headers:x-requested-with');
	//var_dump($_FILES);
	
	//-------------------------------------------------------
	class GetMacAddr{ 
		var $return_array = array(); // 返回带有MAC地址的字串数组 
		var $mac_addr; 
		function GetMacAddr($os_type){ 
			switch ( strtolower($os_type) ){ 
				case "linux": 
					$this->forLinux(); 
					break; 
				case "solaris": 
					break; 
				case "unix": 
					break; 
				case "aix": 
					break; 
				default: 
					$this->forWindows(); 
					break; 
			} 
			$temp_array = array(); 
			foreach ( $this->return_array as $value ){ 
				if ( 
					preg_match("/[0-9a-f][0-9a-f][:-]"."[0-9a-f][0-9a-f][:-]"."[0-9a-f][0-9a-f][:-]"."[0-9a-f][0-9a-f][:-]"."[0-9a-f][0-9a-f][:-]"."[0-9a-f][0-9a-f]/i",$value, 
					$temp_array ) ){ 
					$this->mac_addr = $temp_array[0]; 
					break; 
				} 
			} 
			unset($temp_array); 
			return $this->mac_addr; 
		} 
		function forWindows(){ 
			@exec("ipconfig /all", $this->return_array); 
			if ( $this->return_array ) 
				return $this->return_array; 
			else{ 
				$ipconfig = $_SERVER["WINDIR"]."\system32\ipconfig.exe"; 
			if ( is_file($ipconfig) ) 
				@exec($ipconfig." /all", $this->return_array); 
			else 
				@exec($_SERVER["WINDIR"]."\system\ipconfig.exe /all", $this->return_array); 
			return $this->return_array; 
			} 
		} 
		function forLinux(){ 
			@exec("ifconfig -a", $this->return_array); 
			return $this->return_array; 
			} 
		} 
		$mac = new GetMacAddr(PHP_OS); 
		//echo 'mac地址如下:';
		//echo $mac->mac_addr; 
	//-------------------------------------------------------
	
	
	$dir = iconv('UTF-8','GBK','uploads');
	if(!file_exists($dir)){
		if(mkdir($dir,0777,true)){
			echo '文件夹创建成功!';
		} else {
			exit('文件夹创建失败!');
		}
	}
	
	$path=$dir.'/';
	
	foreach($_FILES as $file=>$con){
		foreach($con as $k=>$v){
			if($k=='tmp_name'){
				$tmpName=$v;
			}
			if($k=='name'){
				$imgName=$v;
			}
		}
	}
	
	if(move_uploaded_file($tmpName,iconv('UTF-8','GBK',$path.time().$mac->mac_addr.rand(10000,99999).$imgName))){
		echo '文件保存成功!';
	} else {
		exit('文件保存失败!');
	}
	

	
?>

結果:
ここに画像を挿入説明
ここに画像を挿入説明
ここに画像を挿入説明
ここに画像を挿入説明
ここに画像を挿入説明

おすすめ

転載: blog.csdn.net/qq_42216575/article/details/89743154