欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

PHP文件上传 (以上传txt文件为例)

程序员文章站 2024-02-19 19:54:10
...

1、前端代码

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>销售数据修改</title>
</head>
<body>
	<div>
		<form enctype='multipart/form-data' id='aa' name='aaa' method='post' action='Platform.php?c=ClarksDataKids&m=uploadFile'> 
			<!-- <input type='hidden' name='MAX_FILE_SIZE' value='2621114' /> --> 
			<input name='file' style="cursor:pointer;" type='file' />
			<input type="submit" style="cursor:pointer;" value="上传修改数据"/>
		</form> 
		
	</div>
</body>
</html>

 2、后端PHP代码

function uploadFile() {
		if (!$_FILES['file']['tmp_name']||!$_FILES['file']['name']){
			echo "<script>alert('请选择要上传的文件!');history.go(-1);location.reload();</script>";
			exit();
		}
		$fileName = basename($_FILES['file']['name']);
		$tempName = $_FILES['file']['tmp_name'];
		
		$date = date("Ymd",time());
		$dir = "/home/wwwroot/erp/FTP/Clarks/POS2ECS/Women/".$date;
		chmod($dir, 0777);	//修改文件权限
		if (!is_dir($dir)){
			mkdir($dir,0777,true);//创建多级目录
			//echo "<script type='text/javascript'>alert('请在有效的时间内执行修改操作!');history.go(-1);location.reload();</script>";
			//exit();
		}
		$newFile = $dir."/".$fileName;
		
		if (is_uploaded_file($_FILES['file']['tmp_name'])){
			$res = move_uploaded_file($_FILES['file']['tmp_name'], iconv("gb2312", "UTF-8", $newFile));
	
			if (!$res){
				echo "<script>alert('上传失败');history.go(-1);location.reload();</script>";
			}else {
				echo "<script>alert('上传成功');history.go(-1);location.reload();</script>";
			}
		}
	
	}