您的位置:首页 > 编程语言 > PHP开发

<php>上传文件的程序

2016-03-03 21:18 686 查看
<!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="touxiangchuli.php" method="post" enctype="multipart/form-data">
<input type="file" name="file" />
<input type="submit" value="上传" />

</form>
</body>
</html>


<!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>touxiangchuli.php</title>
</head>

<body>
<?php
/*echo $_FILES["file"]["name"]."<br>";//获取文件名:6.jpg
echo $_FILES["file"]["type"]."<br>";//获取文件类型:image/jpeg
echo $_FILES["file"]["size"]."<br>";//获取文件大小:6562(字节)
echo $_FILES["file"]["tmp_name"]."<br>";//存储在服务器的临时文件名称:D:\wamp\tmp\php653.tmp
echo $_FILES["file"]["error"]."<br>";//没有错误,返回:0*/

//1.判断是否出错
if($_FILES["file"]["error"])
{
echo "文件上传出错!";
}
else
{
//2.加限制条件
if(($_FILES["file"]["type"]=="image/jpeg" || $_FILES["file"]["type"]=="imge/jpg") &&  $_FILES["file"]["size"]<=102400)//必须是image/jpeg或者imge/jpg格式的,并且文件小于102400字节
{
//3.造一个文件的存储路径
$str = date("YmdHisms",time());//加上时间戳后,可以保证上传的文件名不重复
$filename = "./touxiang/".$str.$_FILES["file"]["name"];
//4.判断文件是否存在
if(file_exists($filename))
{
echo "该文件存在!";
}
else
{
//5.移动文件
move_uploaded_file($_FILES["file"]["tmp_name"],iconv("UTF-8","GB2312",$filename));
echo "该文件成功上传,保存在了".$filename;
}
}
else
{
echo "文件不符合要求";
}
}

?>
</body>
</html>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: