php第二十八节课
文件上传
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
</head>
<body>
<form action="chuli.php" method="post" enctype="multipart/form-data">
<input type="file" name="file" />
<input type="submit" value="上传" />
</form>
<img src="../1-1.jpg" />
</body>
</html>
<?php
//判断文件上传是否出错
if(!$_FILES["file"]["error"])
{
//限制上传文件的类型,文件大小
if(($_FILES["file"]["type"]=="image/png" ||$_FILES["file"]["type"]=="image/jpeg")&& ($_FILES["file"]["size"]<=1048576))
{
//造文件存储的路径
//$filename = "./img/".date("YmdHis",time()).$_FILES["file"]["name"];
$filename = "./img/".$_FILES["file"]["name"];
//处理文件名编码格式
$filename = iconv("UTF-8","gb2312",$filename);
//$filename = "./img/kaifa/zhangsan/20160523/aa.jpg";
//判断文件名是否重复
if(file_exists($filename))
{
echo "该文件已经存在!";
}
else
{
//上传文件(移动文件)
move_uploaded_file($_FILES["file"]["tmp_name"],$filename);
}
}
else
{
echo "上传的文件类型不支持!";
}
}
else
{
echo "文件上传失败!";
}
//php上传头像预览
<?php
//上传文件限制
if ((($_FILES["file"]["type"] == "image/gif")|| ($_FILES["file"]["type"] == "image/jpeg")|| ($_FILES["file"]["type"] == "image/pjpeg"))&& ($_FILES["file"]["size"] < 10000000))
{
//文件上传是否出错
if ($_FILES["file"]["error"] > 0)
{
echo "上传出错: " . $_FILES["file"]["error"] . "<br />";
}
else
{
//输出文件信息
/*echo "文件名: " . $_FILES["file"]["name"] . "<br />";
echo "类型: " . $_FILES["file"]["type"] . "<br />";
echo "大小: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
echo "临时路径: " . $_FILES["file"]["tmp_name"] . "<br />";*/
//判断文件是否已存在
if (file_exists("images/" . $_FILES["file"]["name"]))
{
echo "<script language='javascript'>alert('".$_FILES["file"]["name"]."已经存在!')</script>";
}
else
{
//造存储路径
$filename = "images/" . $_FILES["file"]["name"];
//移动图片至保存路径,解决中文乱码问题
move_uploaded_file($_FILES["file"]["tmp_name"],iconv("UTF-8","gb2312",$filename));
echo "该文件存储在了: " . "images/" . $_FILES["file"]["name"];
echo "<script language='javascript'>parent.showimg('".$filename."')</script>";
}
}
}
else
{
echo "上传失败";
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
</head>
<body>
<form id="sc" action="shangchuan.php" target="hidden_frame" method="post" enctype="multipart/form-data">
<div id="yl" style="margin-left:0px; width:144px; height:170px; background-size:144px 170px; background-image:url(images/%E6%B5%B7%E6%8A%A51101.jpg)">
<input type="file" name="file" style="width:144px; height:170px; float:left; opacity:0;" width="146" onchange="document.getElementById('sc').submit()"/>
<img id="pic" style="display:none"/>
</div>
<input type="button" value="保存"/>
</form>
<iframe style="display:none;" name="hidden_frame" id="hidden_frame">
</iframe>
</body>
<script type="text/javascript">
//回调函数
function showimg(aa)
{
var l = document.getElementById("yl");
var p = document.getElementById("pic");
l.style.backgroundImage = "url("+aa+")";
p.setAttribute("src",aa);
}
</script>
</html>