Ajax multi-file upload

html code is as follows:

<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title>多文件上传</title>
	</head>
	<body>
		<label for="multiFile">请选择需要上传的多个文件:<label>
		<input type="file" name="multiFile[]" id="multiFile" multiple />
		<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();
				var files = document.getElementById('multiFile').files;
				//console.log(files);
				for(var i=0;i<files.length;i++){
					formData.append('files'+i,files[i]);
				}
				//console.log(formData.getAll('files'));
				xhr.send(formData);
				
			}
		</script>
	</body>
</html>

php code is as follows:

<?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 $files=>$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('文件上传失败!');
		}
	}
?>

The results are as follows:
Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description

Guess you like

Origin blog.csdn.net/qq_42216575/article/details/89743540