PHP文件上传技术

在www文件夹下的test文件夹下创建两个php文件(index.php和upload.php)和一个文件夹uploads,上传的文件的内容将存放在uploads文件夹中(如果合法的话)

index.php

<!DOCTYPE html>
<html>
<head>
	<!--mtea charset="UTF-8"-->
	<title>文件上传</title>
</head>
<body>
	<h2>文件上传</h2>
	<hr>
	<form action="upload.php" method="post" enctype="multipart/form-data">
		<input type="file" name="file" id="">
		<br>
		<br>
		<input type="submit" value="上传">
	</form>
</body>
</html>

upload.php

<?php
	//header("content-type:text/html;charset=utf-8");
	echo "<pre>";
	print_r($_FILES);
	echo "</pre>";
	
	//得到文件类型
	$tmpfile = $_FILES['file']['tmp_name'];
	$filetype = explode('/',$_FILES['file']['type']);
	$filetype = $filetype[0];
	
	//得到文件后缀
	$fname = $_FILES['file']['name'];
	$fileext = explode('.',$fname);
	$cnt = count($fileext);
	$fileext = (string)$fileext[$cnt-1];
	
	if($filetype == 'image') {
		$dstfile = 'uploads/imgs/'.time().'_'.mt_rand().'.'.$fileext;
	}
	$dstfile = 'uploads/'.time().'_'.mt_rand().'.'.$fileext;
	
	//允许类型
	$filearr = array("jpg","png","rar");
	
	//限制文件大小
	$filesize = $_FILES['file']['size'];
	
	if(in_array($fileext,$filearr) && $filesize <= 102400) {
		if(move_uploaded_file($tmpfile,$dstfile)) {
			echo "上传成功";
		}
		else {
			echo "上传失败";
		}
	}
	else {
		echo "文件后缀不允许或文件大小超过100KB";
	}
?>


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值