php 之 文件上传(0523)

时间:2023-03-08 22:57:37
php 之 文件上传(0523)

如何上传图片:

上传页面:

 <!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">
<div>
<input type="file" name="file" value="选择文件"/>
<input type="submit" value="上传文件" />
</div>
</form>
</body>
</html>

上传文件处理:

 <?php
//文件上传是否出错
if(!$_FILES["file"]["error"])
{ //限制文件上传类型和大小
if(($_FILES["file"]["type"]=="image/jpeg"||$_FILES["file"]["type"]=="image/png")&&$_FILES["file"]["size"]<=1048576 )
{
//设置文件存储路径,利用
$filename="./img/".date("Ymdhis",time()).$_FILES["file"]["name"]; //处理文件名编码格式,要写在判断文件名的外面
$filename=iconv("UTF-8","gb2312",$filename); //判断文件是否重名或存在 if(file_exists($filename))
{
echo "该文件已经存在!";
}
else
{
//上传文件(移动文件):两个参数---文件临时缓存路径,文件储存路径
move_uploaded_file($_FILES["file"]["tmp_name"],$filename);
} }
else
{
echo "上传文件类型不支持!";
} }
else
{
echo "文件上传失败!";
}

显示运行页面:

php 之 文件上传(0523)

显示文件详细信息:

var_dump($_FILES["file"]);
php 之 文件上传(0523)
name:文件名称
type:文件类型
tmp_name:临时缓存路径
error:文件上传是否出错
size:文件大小