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

1、php文件上传保存。。。

2016-04-19 11:58 357 查看
<!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" name="上传" />
</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>无标题文档</title>
</head>

<body>
<?php

//上传文件的所有信息保存在数组里;
//echo $_FILES["file"]["name"];
//echo $_FILES["file"]["type"];//取文件类型
//echo $_FILES["file"]["size"];//取文件大小
//echo $_FILES["file"]["tmp_name"];//文件在服务器临时存放路径
//
//echo $_FILES["file"]["error"];//判断是否上传出错

//文件上传
//1、判断是否出错
if($_FILES["file"]["error"])
{
echo "文件上传出错";
}else
{
//2、加限制条件
if($_FILES["file"]["type"] =="image/jpeg" && $_FILES["file"]["size"]<1024000 )
{
//处理文件名
$str=date("YmdHisms",time());

//3、造一个存储路径
$url="./img/".$str.$_FILES["file"]["name"];

//将路径的编码格式转换为国标,防止文件名乱码
$FILEname=iconv("utf-8","gb2312",$url);

if(file_exists($_FILEname))
{
echo "该文件已存在";
}else
{
//存储
move_uploaded_file($_FILES["file"]["tmp_name"],$filename);
}
//echo "可以上传";
}else
{

echo "上传不符合要求";
}
}

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