<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="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	mpphp653.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>
原文地址:https://www.cnblogs.com/shark1100913/p/5240114.html