• php文件上传及头像预览


    php文件上传原理是通过form表单的enctype="multipart/form-data"属性将文件临时放到wamp文件夹中的tmp目录下,再通过后台php

    程序将文件保存在体统中。

    html代码:

    1 <form action="shangchuan.php" method="post" enctype="multipart/form-data">
    2     <input type="file" name="file" />
    3     <input type="submit" value="上传" />
    4 </form>

    后台处理界面(shangchuan.php):

    有以下几点需要注意:

    1.控制上传文件的类型
    2.控制上传文件的大小
    3.防止文件名重复
    修改保存的文件名
    用户名+时间戳+随机数+文件名
    流水号

    使用文件夹要提前建好路径。
    4.保存文件

     1 //判断文件上传是否出错
     2 if($_FILES["file"]["error"])
     3 {
     4     echo $_FILES["file"]["error"];
     5 }
     6 else
     7 {
     8     //控制上传文件的类型,大小
     9     if(($_FILES["file"]["type"]=="image/jpeg" || $_FILES["file"]["type"]=="image/png") && $_FILES["file"]["size"]<1024000)
    10     {
    11         //找到文件存放的位置
    12         $filename = "./file/".date("YmdHis").$_FILES["file"]["name"];
    13          
    14         //转换编码格式
    15         $filename = iconv("UTF-8","gb2312",$filename);
    16          
    17         //判断文件是否存在
    18         if(file_exists($filename))
    19         {
    20             echo "该文件已存在!";
    21         }
    22         else
    23         {
    24             //保存文件
    25             move_uploaded_file($_FILES["file"]["tmp_name"],$filename);
    26         }
    27     }
    28     else
    29     {
    30         echo "文件类型不正确!";
    31     }
    32 }

    点击上传后文件就保存在系统的指定路径下。

    保存后按照指定方法重命名文件名:

    头像上传预览:

    原理:在html界面做一个头像大小的div,设置上传头像的背景,在div里面做一个上传文件的input,透明度设置为0.

    这样,点击这个div就可以跟上传的效果相同。

     1 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
     2 <title>无标题文档</title>
     3 <style type="text/css">
     4 #yl{ width:200px; height:300px; background-image:url(img/11.png); background-size:200px 300px;}
     5 #file{ width:200px; height:300px; float:left; opacity:0;}
     6 </style>
     7 </head>
     8  
     9 <body>
    10  
    11 <form id="sc" action="chuli.php" method="post" enctype="multipart/form-data" target="shangchuan">
    12      
    13     <input type="hidden" name="tp" value="" id="tp" />
    14      
    15     <div id="yl">
    16         <input type="file" name="file" id="file" onchange="document.getElementById('sc').submit()" />
    17     </div>
    18      
    19      
    20      
    21 </form>
    22  
    23 <iframe style="display:none" name="shangchuan" id="shangchuan">
    24 </iframe>
    25  
    26  
    27 </body>
    28  
    29 <script type="text/javascript">
    30  
    31 //回调函数,调用该方法传一个文件路径,该变背景图
    32 function showimg(url)
    33 {
    34     var div = document.getElementById("yl");
    35     div.style.backgroundImage = "url("+url+")";
    36      
    37     document.getElementById("tp").value = url;
    38 }
    39  
    40 </script>
    41  
    42 </html>

    php处理界面(chuli.php):

     1 <?php
     2  
     3 if($_FILES["file"]["error"])
     4 {
     5     echo $_FILES["file"]["error"];
     6 }
     7 else
     8 {
     9     if(($_FILES["file"]["type"]=="image/jpeg" || $_FILES["file"]["type"]=="image/png")&& $_FILES["file"]["size"]<1024000)
    10     {
    11         $fname = "./img/".date("YmdHis").$_FILES["file"]["name"];  
    12          
    13         $filename = iconv("UTF-8","gb2312",$fname);
    14          
    15         if(file_exists($filename))
    16         {
    17             echo "<script>alert('该文件已存在!');</script>";
    18         }
    19         else
    20         {
    21             move_uploaded_file($_FILES["file"]["tmp_name"],$filename);
    22              
    23             unlink($_POST["tp"]);
    24              
    25             echo "<script>parent.showimg('{$fname}');</script>";
    26         }
    27          
    28     }
    29 }
  • 相关阅读:
    6Linux用户身份与文件权限
    5Linux流程控制语句-if、for、while、case语句、计划任务
    4Linux环境变量、Vim、Shell脚本
    3Linux常用命令
    2Linux常用命令-Liunu就该这么学
    1安装Linux
    Citrix XenApp工作原理
    Citrix XenApp登录服务器过程详解
    0初识Linux
    我的电脑-磁盘 不显示菜单栏和工具栏解决方法
  • 原文地址:https://www.cnblogs.com/axj1993/p/6579420.html
Copyright © 2020-2023  润新知