php 上传文件

upLoad.php

<html>
	<head>
<!-- 		<link rel="stylesheet" href="myCss.css" type="text/css" /> -->
	</head>
	<div class="register">
		<form enctype="multipart/form-data" method="post" action="upLoadProcess.php" name="myform">
			<table>
				<tr>
					<td align="center" colspan="2"><font style="font-size:40px;font-family:华文彩云">文件上传</font></td>
				</tr>
				<tr>
					<td>请填写用户名:</td>
					<td><input type="text"  name="username"/></td>
				</tr>
				<tr>
					<td>请简单介绍该文件</td>
					<td><TEXTAREA name="fileintro" rows="10" cols="80"></TEXTAREA></td>
				</tr>
				<tr>
					<td colspan="2">请选择要上传的文件:<input type="file" name="myfile"/></td>
				</tr>
				<tr>
					<td align="center" colspan="2"><input type="submit"  value="上传文件" /></td>
				</tr>
			</table>
		</form>
	</div>
</html>
<?php 
	if(isset($_GET['flag'])){
		$flag = $_GET['flag'];
		if($flag == 1){
			echo "<br/<br/><font style='color:red'>文件大小不能超过2M</font>";
		}
	}
?>

upLoadProcess.php

<?php
	//接收提交文件的信息
	if(isset($_POST['username'])){
		$username=$_POST['username'];
	}else{
		echo "上传文件失败";
		exit();
	}
	
	if(isset($_POST['fileintro'])){
		$fileintro = $_POST['fileintro'];
	}
	
	$file_size = $_FILES['myfile']['size'];
	
	if($file_size > 2*1024*1024){
		echo "<font style='color:red'>文件过大,不能上传超过2M的文件</font><br/>";
		echo "<a href='upLoad.php'>返回重新上传</a>";
		exit();
	}
	
	$file_type = $_FILES['myfile']['type'];
	
	if($file_type != "image/jpg" && $file_type != "image/pjpeg"){
		echo "文件类型只能是jpg的";
		exit();
	}
	
// 	echo "<pre>";
// 	print_r($_FILES);
// 	echo "</pre>";
	
	if(is_uploaded_file($_FILES['myfile']['tmp_name'])){
		//把文件转存到你希望的目录;
		$upLoaded_file = $_FILES['myfile']['tmp_name'];
		
		$fileName = $_FILES['myfile']['name'];

		//给每个用户动态的创建一个文件夹
		$user_path = $_SERVER['DOCUMENT_ROOT']."/fileOpt/upLoadFiles/".$username;
		if(!file_exists($user_path)){
			mkdir($user_path);
		}
		
		//$destPath = $user_path."/".$fileName;
		$destPath = $user_path."/".(time().rand(0,1000)).substr($fileName,strrpos($fileName, "."));
		
		//如果目标文件存在,则被覆盖
		if(move_uploaded_file($upLoaded_file,$destPath)){
			echo  $_FILES['myfile']['name']." 上传成功!";
		}else{
			echo "<font style='color:red'>上传失败!</font><br/>";
			echo "<a href='upLoad.php'>返回重新上传</a>";
		}
	}else{
			echo "<font style='color:red'>上传失败!</font><br/>";
			echo "<a href='upLoad.php'>返回重新上传</a>";
	}
	
?>


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值