PHP文件上传

以前学的知识没有 实践就全忘了!  现在吧一些重要的以总结 后用

html

<!DOCTYPEhtml PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<linkrel ="stylesheet" href="../css/my.css" type="text/css" />
</head>
<divclass="register"> <!--文件上传要注意:①enctype ②method post-->

<form enctype="multipart/form-data" method="post"
	action="uploadaction.php" name="myform">
<table>
	<tr>
		<tdalign ="center" colspan="2"> <fontstyle="font-size: 40px;font-family:华文彩云;">
		文件上传 </font>
		</td>
	
	</tr>
	<tr>
		<td>请填写用户名:</td>
		<td><input type="text" name="username" value="123" /></td>
	</tr>
	<tr>
		<td>请简单介绍该文件</td>
		<td><textarea name="fileintro" rows="10" cols="80"></textarea></td>
	</tr>
	<tr>
		<td>请选择你要上传文件:</td>
		<td><input type="file" name="myfile" /></td>
	</tr>
	<tr>
		<td><input type="submit" value="上传文件" /></td>
		<td></td>
	</tr>
</table>
</form>
</div>

</html>

action


uploadaction.php

<?php

//1.接收提交文件的用户
$username = $_POST ['username'];
$fileintro = $_POST ['fileintro'];
date_default_timezone_set ( "PRC" ); // 设置时间
//echo$username.$fileintro;


//我们这里需要使用到$_FILE
/* echo"<pre>";
print_r($_FILES);
echo"</pre>";*/
//获取文件的大小
$file_size = $_FILES ['myfile'] ['size'];
if ($file_size > 2 * 1024 * 1024) {
	echo "文件过大,不能上传大于2m文件";
	exit ();
}

//获取文件的类型
$file_type = $_FILES ['myfile'] ['type'];
if ($file_type != 'image/jpg' && $file_type != 'image/pjpeg' && $file_type != 'image/png') {
	echo "文件类型只能是jpg的";
	exit ();
}

//判断是否上传ok
if (is_uploaded_file ( $_FILES ['myfile'] ['tmp_name'] )) {
	//把文件转存到你希望的目录
	$uploaded_file = $_FILES ['myfile'] ['tmp_name'];
	//我们给每个用户动态的创建一个文件夹
	$user_path = $_SERVER ['DOCUMENT_ROOT'] . "/web/xtickerxsev/file/up/" . $username;
	//$user_path=iconv("utf-8","gb2312",$user_path);
	//判断该用户是否已经有文件夹
	if (! file_exists ( $user_path )) {
		
		mkdir ( $user_path, 0, true );//递归建立文件夹
	}
	//$move_to_file=$user_path."/".$_FILES['myfile']['name'];
	$file_true_name = $_FILES ['myfile'] ['name'];
	$move_to_file = $user_path . "/" . time () . rand ( 1, 1000 ) . substr ( $file_true_name, strrpos ( $file_true_name, "." ) );
	if (move_uploaded_file ( $uploaded_file, iconv ( "utf-8", "gb2312", $move_to_file ) )) {
		
		echo "文件" . $_FILES ['myfile'] ['name'] . "</br>";
		echo "路径" . $move_to_file . "</br>";
		echo "时间" . date ( "YmdHis" );
	
	} else {
		echo "上传失败";
	}
} else {
	echo "上传失败";
}

?>


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值